This section provides a tutorial example on how to write PHP statements and comment in PHP source code files. PHP uses semicolon ';' as the statement delimiter. Comments can be entered in Perl, Java, or C styles.
PHP uses semicolon ";" as the statement delimiter.
PHP statements can be written in the source code file in different ways:
The most common way is to write one statement per one code line.
But you can split one statement into multiple code lines if you have a long statement.
You can also write multiple statements in one code line, if you want to.
If you want to enter comments into the PHP source code, you can do it in 3 ways:
Perl style: Using "#"
Java style: Using "//"
C style: Using "/*" and "*/"
Here is a tutorial example on how to write PHP statements and comments:
<?php /* StatementSamples .php
* Copyright (c) 2003 by Dr. Herong Yang. http://www.herongyang.com/
*/
# The next 1 statement is written in 2 code lines.
print "I am tossing a coin now. "
. "Guess what the result will be?\n";
/* I am using the rand() function to simulate the randomness of
tossing a coin. The rand(min,max) function returns a random integer
between the specified minimum number and maximum number.
-- This is a sample of C style comment
*/
# The next 1 statement is written in 1 code line.
$count = 0;
while ($count < 7) {
# Next 2 statements are written in 1 code line.
$count++; print "Round # $count:\n";
$coin = rand(0,1);
if ($coin==1) {
print " Head\n"; // Java style comment
} else {
print " Tail\n"; # Perl style comment
}
}
?>
There is no surprises in the output:
I am tossing a coin now. Guess what the result will be?
Round # 1:
Tail
Round # 2:
Tail
Round # 3:
Head
Round # 4:
Tail
Round # 5:
Tail
Round # 6:
Head
Round # 7:
Tail