Lesson 3

Learning PHP Syntax

You‘ve seen how PHP can work on a web page. Now it's time to learn some basics of coding in PHP before you get into the detail of the language.

In this lesson you find out about formatting styles for PHP. You learn the general rules of PHP syntax and how to create comments. You learn the specific syntax of different PHP elements as you go over them in subsequent chapters.

Finally, you learn some best practices to make life easier and your code better.

Picking a Formatting Style

When you read a book or type a letter you are used to certain conventions. For instance, each paragraph might have the first line indented and a space before the next paragraph. Each chapter might start with the first letter enlarged.

Styling makes the text easier to understand because it organizes the information and tells you what to expect. If there are no paragraphs, if all the sentences continue one after the other without a break, you can read it but it is more difficult.

Programming uses formatting styles in the same way. The program runs fine without using any formatting but it is more difficult for a human being to read. It is harder to see what is happening and harder to find errors.

A computer has no trouble reading this code:

 <?php $messages=‘';$task=filter_input(INPUT_POST,'task',FILTER_SANITIZE_ STRING);if ($task=='product.maint'):$results=maintProduct();$a==true;$mess ages .=$results;endif;if ('contact.maint'):$results=maintContact();$messa ges .=$results;endif;if ...

Get PHP and MySQL® 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.