Basics of PHP
Written By: Ryan “boxdain” Macy
- 19 May 2006 -
Description: This intro to PHP covers syntax, variables, operators, condition statements, and an intro to arrays.
Introduction
Hello and welcome to my guide to php. What is PHP? Well lets start by what it can do but please note PHP can vary from dynamically created websites to full blown user-interfaces! So I will try to explain to the best of my ability. PHP is a type of server side scripting, which means its processed by the server then sent to the browser to be viewed, this allows user interaction and pages to be generated on the fly. Form validation and user-management are all possibilities of PHP. It allows for faster page creation and can be nested within XHTML. It also can be used with the popular SQL databases.
Things you need before you start to use PHP programming:
- Knowledge of HTML or XHTML.
- A PHP enabled server (you can get a standard apache build with PHP and mySQL for windows at download.com the programs name is phptriad).
Make sure you put all files onto your web site's root directory or c:\apache\htdocs for those self hosting.
Basic Syntax
Pop open your favorite text editor (ex. notepad, Bbedit, etc.) and type in:
<?php phpinfo(); ?>
Upload it to your website or put it in htdocs. Now rename the file index.php, then open up your web browser and type in your website url, or for those who are self hosting type in localhost and it should take you to your page. What you should see is all of your PHP settings and info about your server. If you don't see anything make sure you have PHP correctly installed on your server. If not you can get it and the documentation from php.net.
Now that we know you have PHP installed, let's write some basic code:
<?php print "Hello world"; ?>
what you should get:
Hello world
Well let me explain this further... First off you tell the server you about to use the PHP programming language with <?php then you are using the print function to well print the string to the screen. The quotes tell PHP when the string begins and ends. Then we use the semi-colon (;) to tell PHP we are done with the print function. After that we wrap it all up with ?> to signify that the PHP portion of the page is done, and the server can continue on processing the rest of the website. Let me further explain the different types of the print functions and what they do:
print - can print one alpha numeric string of data to the screen and browser
printf - can print one alpha-numeric string but the string must be enclosed in ( ) brackets
ex. Printf (a string);
echo can be used to print more than one alpha-numeric string (ex. Echo stringOne.stringTwo;). But remember, strings must be separated with a period (.).
Well that's about it for the basic syntax of PHP lets delve into variables....