[Home] [Welcome to PHP] [Data & Dynamic] [File system]

Welcome To PHP

There is one phrase that programmers everywhere like to use when illustrating use of a certain program, and that is "hello world". So wishing not to offend fellow programmers out there, we will use it too here. This page will show you the basics of php scripting.

Your first php script. Create a new file and in it insert the following code, then save it as foo.php.



Pointing your IE/firefox browser to http://yourserver.com/foo.php will show you "Hello World!". There you have your own php script running. Obviously, if it doesn't show that then something is wrong. Either your address, php instalation or spelling. One must also remember to always include the ";" at the end of all php statements. To start with it is good to allow warnings and errors to be displayed on the screen. Check your php.ini file.



Variables

In PHP variables are denoted with $ sign. The following code shows the usage of variables.





IF Statement



If statements are used to compare two values and carry out different actions based on the results of the test. If statements take the form IF, THEN, ELSE. Basically the IF part checks for a condition. If it is true, the then statement is executed. If not, the else statement is executed.

For example, we are testing if the given variable $username is "webmaster" if not..., yeah this is one crappy way of authenticating users but it shows the basic structure of the if statement.





Some other if code that may be usefull is as follows, for example, one may test three things that need to be satisfied for statement to be executed.





WHILE Loop



If you have a piece of code which you want to repeat several times without retyping it, you can use a while loop. For instance if you wanted to print out the words "Hello World" 5 times you could use the following code:





Arrays



Arrays are common to many programing languages. They are special variables which can hold more than one value, each stored in its own numbered 'space' in the array. Arrays are extremely useful, especially when using WHILE loops. Arrays in PHP start from 0 value. To add (or reference) a value within the array, one must specify the location of the element in the array.

Setting up an array of values



Reading from an array is just the same as putting information in. All you have to do is to refer to the array and the number of the piece of data in the array. To go through the array one uses the following code.