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

When I saw the exit() documentation for PHP , I thought:

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

A couple of questions:

  • What are common use cases besides opening files to use exit() ?
  • Since not every function that everyone ever writes ends with exit() , how do you know to use it in some contexts and others?
  • Are if/else and or/exit interchangeable?
+7
source share
4 answers

In this context, or in this expression is one of the PHP logical operators that, when used, executes the second operator if and only if the first is due to a short circuit error.

Since fopen returns false, the or exit statement is triggered because the first part failed.

To understand this better, here is a short description of the short circuit rating.

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

In the above code, the expression $y == 42 never evaluated because it is not necessary because the first expression was true.

In this example, they use the same logic to decide whether to evaluate the statement that calls exit .

To ask your questions:

  • I would not use exit when opening a file if the program was very specific. It would be best to register the error and then return the error to the caller so that they can decide what to do.
  • When to use exit completely depends on the code you write.
  • Given the explanation of the short circuit, yes, they are interchangeable in this sense. Using or exit slightly shorter than using if/else .

Hope this helps.

+3
source
  • CLI scripts, exit can take an integer parameter that returns to the console to indicate success or some form of error.
  • I am not inclined to use exit() or die() in application code, since exceptions are preferred. However, I personally think you can spoil the situation a bit ... it kills the execution of the script, so use it when you need to kill the script. Honestly, I basically only ever killed scripts in the middle of execution when debugging (one-time breakpoints) and that is not perfect (again, exceptions do a better job).
  • Using or is generally more convenient. Here is an interesting point though ...

Why

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

does not work?

The answer is that the = operator takes precedence over or , so the assignment is done as follows: ($resource = mysql_connect()) or die() . So this is exactly the same as doing if(!($resource = mysql_connnect())) { die() }

+4
source

I try to avoid using exit() in general, as this is a really ugly way to handle errors from the user's point of view.

If you must use it, any irreparable error will be a candidate. For example, a database query or connection failures or remote queries.

if/else equivalent ...or whatever() . It is just a style, with the latter form being more concise.

+2
source

I would say that you use exit in a situation where your code cannot continue if the function you are executing has failed. For example, by reading the necessary file.

+1
source

All Articles