Syntax and Comments

Updated onbyAlan Morel
Syntax and Comments

Table of Contents

  1. Syntax
  2. Comments

Syntax

As you saw in our previous lesson where we did a simple Hello World, like so:

PHP
<!DOCTYPE html> <html> <head> <title>Diving into PHP</title> </head> <body> <?php // Display the text 'Hello World' echo('Hello World'); ?> </body> </html>

Hello World in PHP

The core syntax of PHP is actually quite simple. To declare PHP code, simply begin the code with <?php and end it with ?>. Whatever is inside that will be interpreted as PHP code by the web server and executed.

In our example, the actual code being executed is a call to the PHP echo function. What this function does is simply take whatever it is given and echo it on the page. That is why you saw Hello World on the page even though the text wasn't included in a raw HTML form. Because PHP can and is commonly embedded within HTML pages, the final output will appear no different to the user than if the HTML had that text written directly, or hardcoded.

Comments

If you noticed above the call to echo, there was this:

PHP
<?php // Display the text 'Hello World' ?>

That is a comment in PHP. A comment is text that will be completely ignored by the web server as if it were never placed there in the first place. Comments are useful for leaving notes both to yourself and others about the surrounding code.

A comment spanning a single line is done with two slashes // or the hash symbol #, like so:

PHP
<?php // I am a single-line comment # I am also a single-line comment ?>

But you can also write a comment that spans multiple lines by creating a block comment, like so:

PHP
<?php /* I am a block comment */ ?>

Great, that covers basic PHP syntax and how to leave comments!

Next Lesson »
Copyright © 2017 - 2024 Sabe.io. All rights reserved. Made with ❤ in NY.