The absolute most important thing that we need to learn about php syntax are the PHP Delimiters. PHP can only parse code that is within its Delimiters. Anything outside its delimiters is sent directly to the output and not parsed by PHP. There are only two opening delimiters that are allowed by PHP, which are <?php or <?= and only one closing delimiter which is ?>. If the file ONLY contains PHP code, then the closing delimiter can and should be omitted.

The purpose of the delimiting tags are to separate PHP code from non-PHP code (mostly HTML). Everything outside of the delimiters are ignored by the PHP parser and is passed through as output. When writing PHP code, you Absolutely should separate your PHP code from all HTML code.

There are very rare occasions when it is necessary to echo HTML code from within PHP, but this should only be done if absolutely necessary.

The best php semantics when writing code is that you should always only write the code in its original language. This means that if you are going to write JavaScript, you should never have that JavaScript write out HTML and the same goes for PHP. This will always make your code much cleaner and easier to interpret. Here is a list of basic language constructs:

  • All PHP statements should always be terminated with a semicolon (“;”).
  • You can display text with an “echo” statement.
  • All variables must start with a dollar-prefix (“$”) and are case sensitive ($test, $tesT, $newTest, etc.).
  • The assignment operator is “=”.
  • The markup can be modularized with functions (or methods) defined with the keyword “function”.
  • The control structures include: if, while, for, foreach, and switch.

Grouping of control structures can be specified by brackets (“{…}”), but some can use a colon syntax with end keywords, such as in this statement

    if ($x == 0) {
        echo "zero";

or this statement

    while ($x < 5) {
        echo $x;

Comments are another thing that should also be used. There are 3 different ways to do this in PHP. The first being:

    This type of
    comment can
    be spread
    out amongst
    several lines

which serves as block comments which I have displayed. // and # are both used for inline comments meaning they can only be used on a single line.

Now that we have learned a little bit about PHP, lets write out our first PHP statement which is probably the easiest statement that you will ever write.

    echo "Hello World!";

The example above outputs the following: Hello World!. Instead of using . And once again the example above outputs the following: Hello World!.

In my experience, the shortcut above does not always validate as proper PHP syntax. Although it will most likely work, it may not parse as valid PHP code. Please keep this in mind when using this shortcut.

Well, I hope that this article has taught you a thing or two about PHP and its Syntax. If you have any questions, please feel free to ask them in the comments below. I will be happy to answer them.