This post covers the Language Constructs section of the PHP Basics chapter when studying for the Zend PHP 7 Certification.
Language constructs are hard coded into the PHP language. Most of the constructs have unique behaviour, in that they can almost bypass some kind of error handling mechanism.
For instance, isset()
can be used with non-existing variables without causing any notice, warning or error.
function test($param) {}
if (test($a)) {
// Notice: Undefined variable: a
}
if (isset($b)) {
// No notice
}
The die()
and exit()
constructs behave exactly the same. They both terminate the running script.
Note that they can also be used to display output in their parenthesis.
die("Stop running");
exit("Stop running");
You can pass in status codes as an optional parameter. If status is a string, this function prints the status just before exiting.
If the status is an integer, that value will be used as the exit status and not printed. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.
Also, you do not need the parenthesis if option to not pass in a status code.
//exit program normally
exit;
exit();
exit(0);
//exit with an error code
exit(1);
exit(0376); // octal
The echo
construct outputs one or more strings.
echo "Hello World";
echo "This spans
multiple lines. The newlines will be
output as well";
echo "This spans\nmultiple lines. The newlines will be\noutput as well.";
Note that echoing a variable within a pair of double quotes will print out the variable value. Single quotes will print out the variable name.
$foo = "Bar";
echo "The value is $foo"; // Outputs: The value is Bar
echo 'The value is $foo'; // Outputs: The value is $foo
Multiple variables can be echoed.
echo $foo, $bar, $baz;
print
also outputs a string.
<?php
print("Hello World");
print "print() also works without parentheses.";
There are some differences between echo
and print
. The main differences being that print
behaves like a function and echo
without parentheses can take multiple parameters whereas print
can only take 1.
In terms of speed, there is negligible difference between the two.
return
returns program control to the calling module. Execution resumes at the expression following the called module’s invocation.
If called from within a function, the return statement immediately ends execution of the current function.
<?php
function square($num){
return $num * $num;
}
echo square(4); // outputs '16'.
The empty()
construct is used to determine whether the value passed in as an argument is empty.
<?php
$var = "";
var_dump(empty($var)); // Outputs: boolean true
$var = 0;
var_dump(empty($var)); // Outputs: boolean true
$var = null;
var_dump(empty($var)); // Outputs: boolean true
$var = "Some value";
var_dump(empty($var)); // Outputs: boolean false
eval()
is used to evaluate a string as PHP code.
The eval()
language construct can be very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged.
$str = "echo 'Hello World';";
eval($str); // Outputs: Hello World
The include()
and include_once()
constructs include and evaluate the specified file during the execution of the script.
The behaviour for include_once()
is similar to include()
, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once
returns TRUE
.
require()
and require_once
work similar to the include constructs, except that if a required file is not found PHP will emit a fatal error whereas for include only a warning will be emitted.
include()
will throw a warning if it can’t include the file, but the rest of the script will run.
isset()
determines if a variable is set and is not NULL, whereas unset()
unsets a given variable.
<?php
$var = '';
// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
echo "This var is set so I will print.";
}
if (isset($othervar)) {
echo "This var is set so I will print."; // This will not print
}
$var = null;
if (isset($var)) {
echo "This var is set so I will print."; // This will also not print
}
<?php
// destroy a single variable
unset($foo);
// destroy a single element of an array
unset($bar['quux']);
// destroy more than one variable
unset($foo1, $foo2, $foo3);
If a variable passed by references is unset()
inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset()
was called.
The list()
construct assigns variables as if they were an array.
<?php
// The original array
$info = array('coffee', 'brown', 'caffeine');
// Listing all the variables
list($drink, $color, $power) = $info;
echo "$drink is $color and $power makes it special.\n";
// Listing some of them
list($drink, , $power) = $info;
echo "$drink has $power.\n";
// Or let's skip to only the third one
list( , , $power) = $info;
echo "I need $power!\n";
View the other sections:
Note: This article is based on PHP version 7.0.