A Few Facts About PHP
--try each snippet of code provided to see the different results.
1. PHP does have syntactical rules that your code must follow.
2. PHP syntax only applies when it is within PHP tags.
3. PHP is very much like the C programming language.
4. PHP is not sensitive to extra hard-returns, spaces or tabs.
The following 2 lines of script will produce the exact same result:
<?php print ("Do I really have to learn this? Yes you do."); ?> <br> <!-- escaped into html to provide a line break--> <?php print ("Do I really have to learn this? Yes you do."); ?>
5. String text and variables ARE case sensitive within PHP. Other constructs and functions are NOT.
If you type the following lines of code,
<?php print ("This is boring!"); pRINt ("ShoW Me ThE GoodS!"); ?>
Your output will be this:
We will learn about variables later. For now just remember that they ARE case sensitive.
This is boring! ShoW Me ThE GoodS!
6. As shown in the above examples, all PHP statements must be terminated with a semicolon. If not,
you will get a parse error. Try the code below to see the error. Notice that the error provides
you with a line number from your code. With this example, the message tells you the error is on the
that DOES have a semi-colon. If you get a parse error because of a missing semi-colon (as in this example),
the line number in the error message will correspond to a line AFTER the line with the error.
<?php print ("You will get a parse error without a semi-colon") print ("A second line will generate the error"); ?>
7. There are some rules about how to put a PHP statement together. The following 2 paragraphs are
quoted directly from pages 60-61 of the PHP 4 Bible:
The smallest building blocks of PHP are the indivisible tokens, such as numbers (3.14159), strings ("two"), variables ($two), constants (TRUE), and the special words that make up the syntax of PHP itself (if, else, and so forth). These are separated from each other by whitespace and by other special characters such as parenthesis and braces.
The next most complex building block in PHP is the expression, which is any combination of tokens that has a value. A single number is an expression, as is a single variable. Simple expressions can also be combined to make more complicated expressions, usually either by puttin an operator in between (for example, (2 + (2 + 2)), or by using them as input to a function call (for example, power_of(2 * 3, 3 * 2)). Operators that take two inputs go in between their inputs, whereas functions take their inputs in parenthesis immediately after their names, with the inputs (known as arguments) separated by commas.
8. Just as in Algebra, PHP does have some rules as to precedence when it evaluates an expression. I will
not list the rules here. If you write an expression and you are unsure how PHP will evaluate it,
put each expression in parenthesis. In all but one instance ("short-circuiting" Boolean expressions),
you can depend on a left to right evaluation order if each expression is within paranthesis. That
exception will be covered further down the road! For now, try the following code to illustrate mathematical
<?php print 5+2*3; //does the multiplication first, then adds 5 ?> <br> <!--escaped into html to provide a line break <?php print (5+2)*3 //does the parenthesis first, then multiplies by 3 ?>
10. A document using PHP must be saved as .php. If it is not, the server will not know to look for PHP script.
That's the end of Lesson2. Once again, all material covered came from the "PHP 4 Bible" by Converse and Park.
Look for Lesson3 soon!!
Edited by natimage, 17 June 2003 - 08:22 PM.