PHP Syntax

PHP tag starts with <?php and ends with ?>, so in a document it would look like this:

<?php Your code goes here! ?>

Comments

  • Block comments start with a forward slash and asterisk and end in the reverse, like this: /* Comment in here! */
  • Single line comments start with two forward slashes // or #

Other Notes

  • PHP lines end with a semi-colon
  • PHP is case-sensitive (so name and NAME are completely different)
  • You can mix PHP with HTML

HTML and PHP

You can embed PHP code into an HTML document, or you can have PHP pages on their own. If you have PHP code in an HTML document you need to save that document as a PHP file.

Example

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
   <?php 
     echo 'Hello World'; 
   ?>
    </body>
</html>

The PHP code is embedded between the <body> tags, and the file must be saved as a PHP file; otherwise the PHP code will be treated as usual text.

Write Your First Script

Create a new PHP file (Use Notepad++ for all the tutorials, and make sure you save the files in the htdocs folder), and name it FirstScript.php.

PHP Code

<?php
echo 'Hello PHP';
?>

Run it in the browser (localhost:8000/FirstScript.php) and you should see “Hello PHP” print out.

Summary

  • PHP is case-sensitive
  • PHP lines end with a semi-colon
  • PHP single line comments start with two forward slashes // or hashtag (pound sign) #.