1

I've seen this code: defined( 'ABSPATH' ) || exit;

My understanding is that the OR operator returns true if one of them is true. But in this case, exit is a function rather than an assessment to be tested. So is this means that if defined( 'ABSPATH' ) is false, then execute the exit? Why it will execute if it's false?

ctlpquudz
  • 55
  • 4

1 Answers1

1

This is a total guess on my part but my understanding is that this is taking advantage of PHP's lazy evaluation processing. When it encounters values separated by || it will stop evaluating after the first one is true.

For example, the final false will not be checked since the preceding expression is true.

if (false || true || false) { ... }

So, defined( 'ABSPATH' ) || exit; will first evaluate defined( 'ABSPATH' ) and if it is true, it will skip the 2nd expression and the program will continue. Only if it is false will it execute the final expression exit and terminate the program.

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
  • 2
    In addition to this answer: this specific `defined` condition is usually located in the top of the source file and was used in pre-OOP epoch to prevent execution of the source file by calling it by it's path in the browser (the constant should be defined in the separate file that acts as an endpoint of the website). – user1597430 Jun 14 '22 at 02:15