echo 'Name was set to $name
';
unset($name);
} else {
echo 'Name was not set';
}
?>
That script runs the same isset()
check twice, but it unset()
s the variable after the first check. As such, it prints 'Name was set to Ildiko
' and then 'Name was not set
'.
Perhaps the most frequently used function in PHP is exit
, although purists will tell you that it is in fact a language construct rather than a function. exit
terminates the processing of the script as soon as it is executed, meaning subsequent lines of code are not executed. That is really all there is to it; it barely deserves an example, but here is one just to make sure you understand it:
<?php
exit;
echo 'Exit is a language construct!
';
?>
That script prints nothing because the exit
comes before the echo
.
One function we can guarantee you will use often is var_dump()
, which dumps out information about a variable, including its value, to the screen. This is invaluable for arrays because it prints every value and, if one or more of the elements is an array, it prints all the elements from those, and so on. To use this function, just pass it a variable as its only parameter:
<?php
$drones = array('Graham', 'Julian', 'Nick', 'Paul');
var_dump($drones);
?>
The output from that script looks like this:
array(4) {
[0]=>
string(6) 'Graham'
[1]=>
string(6) 'Julian'
[2]=>
string(4) 'Nick'
[3]=>
string(4) 'Paul'
}
The var_dump()
function sees a lot of use as a basic debugging technique because it is the easiest way to print variable data to the screen to verify it.
Finally, we briefly discuss regular expressions, with the emphasis on
The main PCRE functions are preg_match()
, preg_match_all()
, preg_replace()
, and preg_split()
. We start with preg_match()
because it provides the most basic functionality by returning true
if one string matches a regular expression. The first parameter to preg_match()
is the regular expression you want to search for, and the second is the string to match. So, if we wanted to check whether a string had the word
$result = preg_match('/[A-Za-z]est/', 'This is a test');
Because the test string matches the expression, $result
is set to 1
(true). If you change the string to a nonmatching result, you get 0 as the return value.
The next function is preg_match_all()
, which gives you an array of all the matches it found. However, to be most useful, it takes the array to fill with matches as a by-reference parameter and saves its return value for the number of matches that were found.
We suggest you use preg_match_all()
and var_dump()
to get a feel for how the function works. This example is a good place to start:
<?php
$string = 'This is the best test in the West';
$result = preg_match_all('/[A-Za-z]est/', $string, $matches);
var_dump($matches);
?>
That outputs the following:
array(1) {
[0]=>
array(3) {
[0]=>
string(4) 'best'
[1]=>
string(4) 'test'
[2]=>
string(4) 'West'
}
}
If you notice, the $matches
array is actually multidimensional in that it contains one element, which itself is an array containing all the matches to our regular expression. The reason is that our expression has no
Moving on, preg_replace()
is used to change all substrings that match a regular expression into something else. The basic manner of using this is quite easy: You search for something with a regular expression and provide a replacement for it. However, a more useful variant is
PHP manual page URLs take the form http://www.php.net/<()
. As a replacement, we will use the match we found, surrounded in HTML emphasis tags (<em></em>
), and then with a link to the relevant PHP manual page. Here is how that looks in code:
<?php
$regex = '/([A-Za-z0-9_]*)()/';
$replace = '<em>$1</em> (<a href='http://www.php.net/ $1'>manual</A>)';