
This week on Everything PHP, we’ll be looking at PHP’s syntax. The syntax of a programming language is basically a set of rules and symbols for constructing working code.
Starting the Parser
For PHP to be able to parse your code, you must first tell it where your PHP code starts and ends. We do this using starting and ending tags. Which look like this:
<?php ?>
<?php is the starting tag, and ?> is the closing tag. When you’re using PHP inline an (X)HTML document, both the starting and ending tag must be present around your PHP code. However, if you’re using PHP in a standalone file, only the starting tag is required (in this case, it is recommended to omit the closing tag to prevent parsing errors if there’s whitespace after the closing tag).
Variables
Variables are extremely useful things that allow you to give values a shortened name during the runtime of your script. There syntax is as follows:
[code='php']$variable_name = 'Value of variable.';[/code]
Variables always start with a dollar sign ($ — Shift-4 on standard US keyboards), followed immediately by the name of the variable. The variable name must either start with a letter or underscore and may contain letters, numbers and underscores. Variable names are case-sensitive. After the variable name comes a space, then an equals sign (=), then another space and then the value. If the value is a string of text, it must by surrounded by quotation marks. The value doesn’t have to be a string of text, but we’ll talk about that (and what quotation marks [single or double] to use when) in a later post. After the value is the extremely important semi-colon. Without this the sky will fall (your code will result in a parse error, halting your script.
Functions
Functions syntax is as follows:
[code='php']function_name( $argument1, 'argument 2', 1 );[/code]
Functions always start with the name of the function (which is case-insensitive), then an opening parenthesis and then the arguments you are supplying to the function (not all functions require arguments). Arguments are separated with commas. I’ve provided three examples of arguments here, a variable, a string and a binary argument. After the arguments is a closing parenthesis and then a semi-colon. Once again, we will be going over this stuff in more detail in a later post.
Language Constructs
Language constructs are setup a bit differently than functions.
[code='php']echo 'Hello World!';
echo $variable;[/code]
Language constructs start with the name of the construct (in this case echo which outputs text to the webpage), which is then followed by a space and a value. On the first line, the value is a string of text and on the second the value is a variable. After the value, is a semi-colon.
Control Structures
Control Structures are one of the most important things in PHP, allowing you to test expressions, start loops and more. Their usual syntax is as follows:
[code='php']if ( expression ) {
statement
statement
}[/code]
Control Structures start with the name of it, followed by an expression (for example testing if something is true or not) enclosed within brackets, which is then followed by statements (variables, functions and language constructs are all statements) enclosed in curly brackets { } (or “sexy brackets” as my Science 8 teacher called them). As this is not a statement, no semi-colon is necessary after this (they still are in the contained statements, however).
In the Coming Weeks
In the next few week’s posts we’ll be talking in-depth about variables, functions, language constructs and control structures. We’ll also be talking more about how this all fits together. Soon after that, we’ll discuss debugging your code and how to decipher PHP’s cryptic error messages.
PS: If you’re reading this in a Feed Reader, head over to the original post to see the code examples posted with syntax highlighting, line numbers and an easy copy to clipboard option.




