Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this PHP syntax correct?

I am currently studying ZEND framework and came across this in index.php:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

Is this the same as this?

if(!defined('APPLICATION_PATH')){
    define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
}

I have never came across this type of shorthand syntax before.

like image 791
Stann Avatar asked Nov 10 '10 20:11

Stann


People also ask

What is the syntax of PHP code?

Basic PHP Syntax A PHP script can be placed anywhere in the document. The default file extension for PHP files is " .php ". A PHP file normally contains HTML tags, and some PHP scripting code.

Is PHP whitespace sensitive?

PHP is whitespace insensitive PHP whitespace insensitive means that it almost never matters how many whitespace characters you have in a row. one whitespace character is the same as many such characters.


2 Answers

|| is a short-circuit operator which means that the second operand, in this case define(...) is only evaluated in case the first operand evaluates to false. Because operands to shirt-circuit operators may actually have side effects as is your case, short-circuiting may substitute an if statement.

Check this article: http://en.wikipedia.org/wiki/Short-circuit_evaluation

like image 115
Blagovest Buyukliev Avatar answered Sep 21 '22 21:09

Blagovest Buyukliev


Functionally, yes, it is the same. The defined function returns a boolean, so it uses short-circuit evaluation to mean "either this is defined, OR execute this definition."

like image 37
Tesserex Avatar answered Sep 24 '22 21:09

Tesserex