Language Basics

This section is a short introduction to the basic syntax of PHP. If you’re familiar with high-level languages such as C, C++, Java, JavaScript, or Perl, then you’ll find PHP very easy to learn. PHP began its life as a simple, procedural language that was designed to be embedded into HTML pages, and that’s mostly what we focus on this chapter. More recently, it has become a full-fledged object oriented (OO) language that can be used for nonweb and cross-platform applications, including both scripts that run from the command-line and programs with graphical user interfaces (see the PHP-GTK web site, http://gtk.php.net). You’ll find pointers to comprehensive PHP resources in Resources” at the end of this chapter.

As discussed previously, PHP scripts are surrounded by the PHP start tag <?php and the PHP end tag ?>. You’ll sometimes see the start tag abbreviated to <?, but this conflicts with the 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 */

Anything that appears after the // or #, or between the /* and */ characters, is ignored.

Variables are prefixed with a dollar sign ($) and variable names are case-sensitive. PHP has several variable ...

Get Learning MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.