A Short Language Primer
This section introduces the basic syntax of PHP. If you’re familiar with high-level languages such as C, Java, JavaScript, or Perl, you’ll be at home with PHP. The current version of PHP is PHP 4, and some details we present here are specific to this version.
As discussed previously, PHP scripts are surrounded by the
PHP start tag
<?php and the end tag ?>.
You’ll often see the start tag abbreviated as
<?, but this conflicts with the emerging XHTML
standard and should be avoided.
Statements in a script are terminated with a semicolon. Statements can be formatted for readability by including any amount of whitespace—such as space characters, tab characters, or blank lines—in a script.
Comments can be included in a PHP script using the following styles:
// One line comment # Another one line comment /* A multiple line comment */
Data can be output with the statements
print
,
echo
, and
printf
.
The first two are often interchangeable, but echo
has an advantage in that it can take more than one argument. The
printf statement is used for more complex output
and is identical to that used in other programming languages such as
C and scripting languages such as awk. Consider
a few examples:
// These are the same
echo "This is output";
print "This is output";
// echo can output more than one argument
echo 123, "is a number";
// printf can be used to control formatting
// This outputs 3.14
printf("pi is %.2f\n", 3.14159);Variables are identified by the prefix ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access