<< PHP Beginner Tutorial | PHP Tutorials | PHP Variables >>

PHP Syntax

PHP's syntax (The rules governing the formation of statements in a programming language - dictionary.com) is similar to that of other programming languages such as C/C++ and Perl. To make any PHP code, it must be between the one of the following:

<?php
?>

<?
?>

It is important to note that the second choice above should not be used in any script you intend to give out to anyone. The second tags are called short tags and are only enabled on some servers, so you should get in the habit of using the first choice (as it will be shown in this tutorial).

Line Terminator and Whitespace

In PHP, when ending an expression in PHP a semicolon must be at the end to tell the parser. If it isn't there, an error will come up when you try to run your script. For example, here is a little bit of PHP:

<?php
if ($variable) {
echo 'Hello';
}
?>

Notice how after the if ($variable) { and } there were no semicolons. These are not expressions. The echo 'Hello' part is, and must be finished by a semicolon. You will be able to see more of this throughout the whole tutorial.

Whitespace in PHP is ignored completely by the parser (as well as tabs). This makes it great for programmers, as it can help us organize our code better plus it makes it look better. Having said that, this:

<?php
if($variable){echo'Hello';}else{die();}
?>

Looks exactly the same as this (to the parser):

<?php
if ($variable) {
   echo  'Hello' ;
} else
{
 die();    }
?>

Although I don't suggest either of them, because it can make your code completely unreadable to not only you, but anyone else who tries to read it :S.

The Curly Brace!!! :O

There are some people (if you ask them) who will tell you they don't need the curly braces }, { in their code most times. Well I'm here to tell you my opinion.. and that is to use them. I think it makes code look more organized, and who doesn't like organized code? Seriously. Anywhom, curly braces in PHP signify a block of code. You have seen them in almost all the previous examples. I won't go on about them anymore right now, because you will learn about them more later on in the tutorial. Just remember that you should use them, instead of not.

<< PHP Beginner Tutorial | PHP Tutorials | PHP Variables >>