An Introduction to PHP

Getting Started

PHP scripts have the extension .php. The PHP script is placed between the <?php and ?> delimiters. The script that is placed between these delimiters is interpreted on the server into HMTL, before being sent to the client. The client will receive a pure HTML page.

Comments

You can comment your code using C++ type comments. The following shows the syntax for inline comments (a comment on a single line), and block comments (comments placed over several lines).

// This is an inline comment
/*
This is a block comment
placed over two or more
lines
*/

Output

There are two basic statements to output text with PHP, either using echo, or print.

Output Using echo

The text to be outputed follows the echo statement.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Starting PHP</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
  <?php echo "Hello World<br>"; ?>
</p>
</body>
</html>

Output using print

The text to be outputed follows the print statement.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Starting PHP</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
  <?php print "Hello World<br>"; ?>
</p>
</body>
</html>

PHP also offers a shothand way of outputting information, using the <?= and ?> delimiters. The following example prints the variable $name (passed from a form) to greet a user.

Hello <?=$name?>

PHP Information

The phpinfo function is useful for trouble shooting, providing the version of PHP, and how it is configured.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>PHP Information</title>
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<p>
  <?php phpinfo();?>
</p>
</body>
</html>

You might also like...

Comments

About the author

Gez Lemon United Kingdom

I'm available for contract work. Please visit Juicify for details.

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“XML is like violence - if it's not working for you, you're not using enough of it.”