Basic Syntax

only for RuBoard - do not distribute or recompile

Basic Syntax

PHP generally follows the C syntax, with a little flavor of Perl shown during string handling. All statements end with a semicolon. Algebraic-style formulas are used for mathematical operations. You have the standard C-like printf function. However, under PHP, strings delimited with " are expanded on-the-fly .

For example, to write the formula E=MC^2 (E equals M times C squared) in PHP:

 $E = $M * ($C * $C); 

The next statement shows available math functions, including a function call which works just like its C counterpart :

 $result = ($a * $b) / $c + bcpow(($d - ($e % 5)),3,2); 

This function multiplies a by b, and then divides the result by c. It then takes the remainder of e divided by 5, and subtracts that from d. That result is given to the bcpow() function, which raises the result to the third power and returns that value. That value is then added to the result of the multiply and divide part of the equation and the answer is assigned to the variable $result.

Comparisons are done with special operators. All comparison operators consist of two characters side by side from this list: =, <, !. You can test for equal to ( == ), greater than (), less than, less than or equal to, greater than or equal to, not equal to ( != ), identical to (both in value and type) ( === ) or not identical to ( !=== ).

In PHP 4, you can compare arrays using the equality operator ( == ) or the identity operator ( === ):

 $myArray = array ( 4, 2, 1, 3, 5);   $myArray2 = array ( 1, 2, 3, 4, 5);   $myArray3 = array ( 4, 2, 1, 3, 5);   if ($myArray == $myArray3)      echo "$myArray is equal to $myArray3<BR>";   if ($myArray === $myArray3)      echo "$myArray is identical to $myArray3<BR>";   if ($myArray == $myArray2)      echo "$myArray is equal to $myArray2 <BR>"; 

The previous code displays:

 $myArray is equal to $myArray3 $myArray is identical to $myArray3 

The PHP parser will take a string and expand any variables within that string before it uses the string. If a string is delimited with single quotes, the PHP parser will leave the string alone. Strings are put together with the concatenation operator, which is a period, (.).

You are allowed to put commonly used code in subroutines. These subroutines are called functions. You can define your own functions or use functions included in the PHP library. The function calls follow the C syntax.

only for RuBoard - do not distribute or recompile


MySQL and PHP From Scratch
MySQL & PHP From Scratch
ISBN: 0789724405
EAN: 2147483647
Year: 1999
Pages: 93
Authors: Wade Maxfield

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net