Code:PHP is a server side scripting language that makes it easy to create dynamic websites. The code is similar to Java and C++ so if you know one or both of these languages you should be able to pick up PHP fairly easily. To use PHP you must either upload your site to a server that supports PHP or set up your own server. Unless you have a fast internet connection uploading your files for testing all the time will most likely not be a very good option for you. A good option is to have your own server on your local workstation. The best way to do this would be to use Windows 2000 or XP with Internet Information Services installed. You can then download the PHP engine and install it on your computer. This will allow you to test your pages quickly. However there are other ways of doing this such as the Apache sever. PHP is an embedded language. This means that you can embed code within a HTML document. There are a few ways in which this can be done: <? echo ”Test Message”; ?> This is the most common way of embedding code, but it may cause problems if your code must co-exist with XML. Another common way to embed is: <?PHP echo ”Test Message”; ?> I recommend using this method for embedding for compatibility with other systems. There are two other alternate methods but these two aren’t used very often: <% echo ”Test Message”; %> By default this method is turned off, and I do not recommend using it. The final way is similar to putting JavaScript in your page: <SCRIPT LANGUAGE=”php”> echo ”TestMessage”; </SCRIPT> Every file you create that contains PHP code you must make sure the extension is the PHP extension set on your server. I usually just use .php, but some people say that you should use the PHP version .php3 or .php4. I have never had any problems but there may be some issues with other servers which is why I still mentioned it. As I mentioned earlier PHP’s syntax comes from languages like C++. This means that you can use: // /* */ and even # for comments. Also the structure of things like for loops are similar, but variables in PHP are different. All variables have a $ sign at the start and is followed by an alphabetic or underscore character. No data type declarations are necessary and you do not have to initialise the variables: <? $name; $name=”Edwin”; echo “My name is $name”; ?> OUTPUT: My name is Edwin Notice how I can just put the variable name inside the output string. Double quotes will be checked for variables and escape sequences. If you do not want this to happen you would use single quotes: <? $name; $name=”Edwin”; echo ‘My name is $name’; ?> OUTPUT: My name is $name If you do wish to specify the data type for a variable you can use type casting: $number = (int) = “342asd”; This will create an integer value of 342. The types PHP supports are as follows: (int), (integer) – Cast to integer (real), (double), (float) – Cast to float (string) – Cast to string (array) - Cast to array (object) - Cast to object Selection is something that is very important to know for every language. Selection in PHP is very similar to C++ and Java. We will fist start with the If statement: if(expression){ statements } This is a basic If statement. It is a bit hard to understand this way because we have nothing to test for so I will expand and put some sample data in. if($name == “Edwin (Zxeenon)”){ echo “Zxeenon is the creator of this tut”; } Now with this data in it will say if the value in $name is equal to the string “John” then do the code within the braces. We can then further expand on this to do something else if the value is not true: if($name == “Zxeenon”) { echo “Edwin is the creator of this tut”; } else { echo “$name is not the creator of this tut”; } This code will do the same as previous but it will now output the second echo statement if it is not true. Say the value of $name is “Bob”. It will go through and say is $name the same as “John”. No its not so we will go to else and output “Bob is not the creator of this tut”. Getting confused? I hope not because we are going to expand on this code again and test for a second name. if($name == “John”){ echo “John is the creator of this tut”; } else if($name == “Bob”){ echo “I don’t know any Bob”; } else { echo “$name is not the creator of this tut”; } This code again does the same as previous but it will test for two conditions. Say the value of $name is still “Bob”. It will look at the first condition. Is the value in $name the same as “John”? No, So go to the next if. Is the value in $name the same as “Bob”? Yes, output “I don’t know any Bob”. You can keep expanding on this testing for as many conditions as you wish. Just a note if you have only one line after you if statement you don’t have to have braces: if($name == “John”) echo “John is the creator of this tut”; But if you want two or more statements you must have braces. I just find it easier to use braces all the time. Also you may be wondering why I am using “==”. Well if you think to where you assign a value: $name = “John”; That is how you assign a value, so if you had: if($name = “John”) You would be trying to assign a value and you would receive an error. Other comparisons you can do are things like &&, ||, >, < I will explain the Boolean operators in a later tutorial. The next selection statement is the Switch. The basic syntax is as follows: switch(expression) { case expression: statements break; default: statements break; } Again this is a bit hard to follow so I will put some meaningful code in to help: switch($name) { case “John”: echo “John is the creator of this tut” break; default: statements break; } This will basically do the same sort of thing as an if statement. If the value in $name is “John” then output “John is the creator of this tut”. If not then go to the next case, in this example the next case is the “default:”. This acts in a similar way to “else” in an if statement. Also just like if statements you can expand and test for more values. To do this all you need to do is to add in more case statements. switch($name) { case “John”: echo “John is the creator of this tut” break; case “Bob”: echo “I don’t know Bob” break; default: statements break; }Btw my real name is Edwin so thats why you should know.
Results 1 to 2 of 2
- 25 Nov. 2009 07:04pm #1
[TEACHING]The Basics of [PHP] Documents
- 26 Nov. 2009 04:08am #2
This is really helpful. Very very helpful.
I really hope you do more of these!If you would like a Lockerz.com invite, simply PM me your email, and I'd be happy to invite you.