PHP Syntax Demo

The PHP language uses a syntax very similar to the C language. Each PHP instruction starts with <? and ends with a >. Or, instructions may be grouped inside a single <? > pair and separated by ; characters.

Variables are supported and are indicated by preceding the variable name with a $. So, for example, to set a variable to 5 and then display this variable, the following is valid:

<?$a = 5>
<?echo $a>

This is the same as writing:

<?$a = 5; echo $a>

Or even:

<?
$a = 5;
echo $a;
>


Extra white space characters such as spaces, tabs and new-lines are ignored. Case is relevant in variable names, but not in function calls.

Comments are supported. A comment is written just like comments in the C language. /* starts a comment and */ ends a comment.

Three types of variables are supported. Long integer, Double precision floating point and character strings. They are automatically detected. For example:

<?$a = 5>

causes $a to become a variable of type INTEGER.

<?$a = 5.0>

causes $a to become a variable of type DOUBLE.

<?$a = "5">

causes $a to become a variable of type STRING.

The type of the variable is not generally important. Every variable regardless of its type is converted to all three types internally and the various functions will try to use the correct type. There are only a few functions affected by the type of the variable.

All three variable types may also be treated as arrays by appending [value] to their names. Unlike C, these are actually named arrays similar to those used in Perl. The following would be valid:

<?
$a[0] = 5;
$a["hello"] = 6;
echo $a[0];
echo $a["hello"];

>

As far as language constructs are concerned, the PHP language is quite simple. The following commands are used to guide the control flow through a file:

The syntax of conditions are similar to that of the C language. == tests for equality. != means not equal. Also supported are: >, <, >=, <=. Conditional AND is &&, conditional OR is ||. Examples:

<?
if($a==5 && $b!=0 );
	$c = 100 + $a / $b;
endif;
>
Note that another difference between the PHP language and the C language is that no curly braces are used and that you need to put a separator after if and while statements.

Also introduced in the above example was a mathematical expression. PHP supports full mathematical operations anywhere an expression is expected. Order of operations are considered as well.

<?
$a=0;
while($a<100);
	$a++;
	echo $list[$a];	
endwhile;
>
This example shows the use of a while loop to display the contents of an array. WARNING although the PHP language supports incremental operators such as ++ and -- to increment and decrement a variable, they are not treated exactly like they would be in the C language. The variable is incremented right away. There is no concept of incrementing the variable before or after the operation as there is in C.

<?
$a=0;
switch($a);
	case 1;
		echo "a is 1";
		break;
	case "hello";
		echo "a is hello";
		break;
	default;
		echo "a is unknown";
		break;
endswitch;
>
The above is an example of a switch construct. It is similar to a series of if/elseif/else constructs but easier to read. The only difference between the PHP switch construct and that of the C language is that semi-colons are used to terminate every line. There are no braces nor colons.

All of these various constructs can of course be nested and used inside each other just like C.