Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why if/else vs. or/exit in PHP?

Seeing the exit() PHP documentation got me thinking:

$filename = '/path/to/data-file';
$file = fopen($filename, 'r')
or exit("unable to open file ($filename)");

Couple questions:

  1. What are common use cases besides opening files for using exit()?
  2. Since not every function everyone ever writes ends in exit(), how do you know to use it in some contexts vs. others?
  3. Are if/else and or/exit interchangeable?
like image 486
tim peterson Avatar asked Jul 15 '12 21:07

tim peterson


People also ask

What does exit () do in PHP?

The exit() function prints a message and terminates the current script.

What is the difference between die () and exit () in PHP?

The exit() method is only used to exit the process. The die() function is used to print the message. The exit() method exits the script or it may be used to print alternate messages.

What does => mean in PHP?

It means assign the key to $user and the variable to $pass. When you assign an array, you do it like this. $array = array("key" => "value"); It uses the same symbol for processing arrays in foreach statements. The '=>' links the key and the value.

How do you use exit in if statement?

Exit an if Statement With the Function Method in Python We can use an alternative method to exit out of an if or a nested if statement. We enclose our nested if statement inside a function and use the return statement wherever we want to exit.


Video Answer


2 Answers

In that context, the or in that statement is one of PHP's logical operators which when used like that, will execute the second statement if and only if the first one fails due to short circuit evaluation.

Since fopen returned false, the or exit statement gets executed since the first part failed.

To understand it better, here is a quick explanation of short-circuit evaluation.

$x = 5;
$y = 42;

if ($x == 5 or $y == 42) {
    echo "x or y is true";
}

In the above code, the expression $y == 42 is never evaluated because there is no need since the first expression was true.

In that example, they are using the same logic for deciding whether or not to evaluate the statement that calls exit.

To address your questions:

  1. I wouldn't use exit when opening a file failed unless the program was very specific. The better thing to do would be to log an error and then return the error to the caller so they can decide what to do.
  2. When to use exit completely depends on the code you are writing.
  3. Given the explanation about short-circuiting, yes they are interchangeable in that sense. Using or exit is just a bit shorter than using if/else.

Hope that helps.

like image 138
drew010 Avatar answered Sep 22 '22 17:09

drew010


  1. CLI scripts, exit can take an integer parameter which is fed back to the console to indicate success or some form of error
  2. I'm not inclined to use exit() or die() in application code, since exceptions are preferred. However, I personally think you might be overcomplicating things a little bit... it kills script execution, so use it when you need to kill a script. Truthfully I mostly only ever kill scripts mid-execution when debugging (one-off breakpoints) and that's not ideal either (again exceptions do a better job).
  3. The use of or is mostly convenient. Here's an interesting point though...

Why does

$resource = mysql_connect() || die('dead') 

not work?

The answer is that the = operator takes precedence over or so that the assignment is made first like so: ($resource = mysql_connect()) or die(). In this way its exactly like doing an if(!($resource = mysql_connnect())) { die() }

like image 33
dianovich Avatar answered Sep 20 '22 17:09

dianovich