void exit ( [mixed status])
mixed eval ( [string code_string])
void die ( [mixed status])
Two simple functions give your scripts much more power: exit() and eval() - both are very easy. Exit() takes just one optional parameter, and terminates execution of the script immediately. If you pass a parameter, this is used as the script exit code, and, if it is a string, is printed out. Note that the function die() is an alias of the exit() function and works the same way. Eval() is another function that takes one string parameter, but it executes that string as if it were PHP code. Here are examples of both exit() and eval() in action:
<?php
$str = "exit()";
eval($str);
?>
For the pedants out there: exit() is not technically a function, it's a language construct.
That script assigns "exit()" to $str, then passes $str into eval(). As I mentioned, eval() will execute the string it gets as if it were PHP code, so the end result is that PHP runs the exit() function through eval(), and terminates the script.
Exit() is a very common function, and is used wherever you need to end a script with no further work. For example:
if ($blah != $blahblah) {
print "Access denied.";
exit;
}
Eval() might seem a little pointless to you, but it is actually very helpful - it allows you to pass in any text string as PHP code and have it executed. This allows you to store your PHP code in a database, or to calculate it on the fly, which gives you a lot more flexibility.
The exit() function takes a maximum of one parameter, which can either be a program return number or a string. Many programs return numbers so that they can be chained to other programs and their output properly judged. In this case, 0 usually means, "everything went OK", and everything else means, "something went wrong". Using exit() with a string causes PHP to output the string then terminate the script - a behaviour commonly used by programmers with exit()'s alias, die(), like this:
do_some_func() OR die("do_some_func() returned false!");
In that situation, do_some_func() will be called, and, if it returns false, die() will be called to terminate the script. This is a fast and easy way to make sure that certain functions have run successfully before you get into the meat of your script. The reason that code works is because the OR operator means that PHP will only execute the second function if the first function returned false.
Note that this is the most common use for the OR operator. If you recall from earlier, the || operator comes higher than the OR operator in terms of precedence, and, more importantly, || also comes higher than =.
Now, take a moment to think this through: what does this next line of code do?
$fp = fopen("somefile", "r") || die("Could not open file!");
On the surface, it looks like our previous example of die() - PHP will try to load the file "somefile" and store a file pointer to it in $fp, and, if it fails, will exit with the message "Could not open file!"
However, that is not actually what is done. Instead, because || has a higher precedence than =, it is calculated first. So, first PHP will attempt to load somefile. If it succeeds, fopen() will return a resource that evaluates to true. Naturally all calls to die() succeed, so die() will also always be evaluated as true. However, because PHP will short-circuit the || operator, when fopen() succeeds die() will not be called and the || operator will return 1 (true) because the operand on its left side (the call to fopen()) returned true.
Essentially, PHP reads the line of code like this:
$fp = (fopen("somefile", "r") || die("Could not open file!"));
Finally, this 1 will be assigned to $fp. This is clearly not what was intended, and this is where the OR operator comes in. As OR has a lower priority than =, PHP reads the line of code like this:
($fp = fopen("somefile", "r")) or die("Could not open file!");
That is, fopen() is called and its value assigned to $fp, which is then put through the comparison operator. As you can see, there really is a need to have both || and OR around, but just please make sure you pick the correct one!
Author's Note: I suggest you keep the following quote in mind from the creator of PHP, Rasmus Lerdorf: "If eval() is the answer, you're almost certainly asking the wrong question." That isn't to say that eval() is bad, merely that you should think twice if not three times before using it.
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Working with Date and Time >>
Previous chapter: Working with variables
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.