This chapter focuses on the nitty gritty details of PHP as a language. This is a big chapter and it covers one of the most important aspects of the exam. The best advice that I can give is not to skim over it. There are a lot of places where PHP has some peculiarities and even with a few years of experience you might not have encountered them all.
All statements in PHP must be terminated with a semicolon. An exception to this is if the statement happens to be the last statement before a closing tag.
Whitespace has no semantic meaning in PHP. There is no need to line code up, but most coding standards enforce this to improve code readability. Whitespace may not appear in the middle of function names, variable names, or keywords.
Multiple statements are allowed on a single line.
Code blocks are denoted by the brace symbols { }.
PHP language construct and function names are not case-sensitive, but variable and constant names are.
<?php
ECHO "Hello World"; // works
$variable = "Hello World";
echo $VARIABLE; // won't work
Although PHP is a general scripting language and can be used for other purposes, it is most commonly deployed as a server-side scripting language used for building web pages.
The PHP parser does not parse anything that is not included in the tags that indicate PHP script. Content outside of PHP tags is simply output without inspection. This allows PHP to be embedded in HTML.
There are several ways to delimit PHP script, but only the first two in this table are commonly used:
Type | Open | Close | Note |
---|---|---|---|
Standard | <?php | ?> | |
Echo | <?= | ?> | |
Short | <? | ?> | Deprecated |
ASP | <% | %> | Deprecated |
The echo tag allows you to easily echo a PHP variable and the shortened tag makes your HTML document easier to read. It is commonly used in templates where you want to output several values into various positions on a page. Using the short syntax keeps your template code much neater.
Its usage is easiest to understand when it’s shown along with the equivalent in standard opening codes. The following two tags are identical:
<?php
<?= $variable ?>
<?php echo $variable ?>
Note
The echo statement is the last statement before a closing tag and so it does not need a semicolon to terminate it.
You can use PHP logic between opening and closing tags, as in this example:
Balance:
<?php
if ($bankBalance > 0): ?>
<p class="black">
<?php else: ?>
<p class="red">
<?php endif; ?>
<?= $bankBalance?>
</p>
Let’s step through the code:
-
The PHP parser will output Balance: without evaluating it because it is not in the PHP tags.
-
The PHP tag then checks if the balance is greater than zero and terminates. The
tag is only output if that condition is true; otherwise, the
tag is output.
-
We use the echo tag syntax to output the $bankBalance variable.
-
Finally, the closing paragraph tag is output without being parsed because the PHP script has been closed.
Note: This approach will also work using the curly brace syntax of an if statement.
It is quite common in PHP programs to omit the closing tag ?> in a file. This is acceptable to the parser and is a useful way to prevent problems with newline characters appearing after the closing tag.
These newline characters are sent as output by the PHP interpreter and could interfere with the HTTP headers or cause other unintended side-effects. By not closing the script in a PHP file, you prevent the chance of newline characters being sent.
Tip: It is a common coding standard to require that the closing tag is omitted in included files, but this is not a PHP requirement.
Language constructs are different from functions in that they are baked right into the language.
Language constructs can be understood directly by the parser and do not need to be broken down. Functions, on the other hand, are mapped and simplified to a set of language constructs before they are parsed.
Language constructs are not functions, and so cannot be used as a callback function. They follow rules that are different from functions when it comes to parameters and the use of parentheses.
For example, echo doesn’t always need parentheses when you call it and, if you call it with more than one argument, then you can’t use parentheses.
array(1) { ["baz"]=> string(10) "Murky code" } } */ There are several caveats to using variable variable names. They could impact on your code security and can also make your code a little murky to read. ### Checking If a Variable Has Been Set The command isset() will return true if a variable has been set and false otherwise. It is preferable to use this function instead of checking if the variable is null because it won’t cause PHP to generate a warning. The command empty() will return true if a variable is not set and will not generate a warning. This is not a bulletproof way to test if a variable is set. Note Remember that the string “0” is considered empty, but is actually set. Variables become unset when they become out of scope and you can use the command unset() to manually clear a variable. We’ll see later in the book that the garbage collector is responsible for freeing up the memory allocated to variables that have been unset. ## Constants Constants[7](file:///home/antonio/Escritorio/PHP 7 Zend Certification Study Guide - Andrew Beak.htmlz_FILES/index.html#calibre_link-522) are similar to variables but are immutable. They have the same naming rules as variables, but by convention will have uppercase names. They can be defined using the define [8](file:///home/antonio/Escritorio/PHP 7 Zend Certification Study Guide - Andrew Beak.htmlz_FILES/index.html#calibre_link-523) function as shown: 1.6, 'INCHES_CONVERSION' => '2.54']); echo "5km in miles is " . 5 * UNITS['MILES_CONVERSION']; /* 3.1425km in miles is 8 */ The third parameter of define is optional and indicates whether the constant name is case sensitive or not. You can also use the const keyword to define constants. Constants can only contain arrays or scalar values and not resources or objects. 1.6, 'INCHES_CONVERSION' => '2.54']; echo "5km in miles is " . 5 * UNITS['MILES_CONVERSION']; /* 5km in miles is 8 */ Only the const keyword can be used to create a namespaced constant, as in this example where we create constants in the "Foo" namespace and then try to reference them in the "Bar" namespace.