Possible Duplicate:
Why 'defined() || define()' syntax in defining a constant
This piece of code is created by the zf tool that Zend Framework provides.
defined('APPLICATION_PATH') || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
My questions is : What is the purpose of this line of code? There are no conditional statements such as if, switch. Does it imply a conditional statement automatically?
Here is how i understand it:
if APPLICATION_PATH is defined, leave it alone else set it to : realpath(dirname(__FILE__) . '/../application')
.
If my assumption is true this is a really confusing syntax.
Any help will be appreciated.
Description ¶ defined(string $constant_name ): bool. Checks whether the given constant exists and is defined. Note: If you want to see if a variable exists, use isset() as defined() only applies to constants. If you want to see if a function exists, use function_exists().
The double arrow operator, => , is used as an access mechanism for arrays. This means that what is on the left side of it will have a corresponding value of what is on the right side of it in array context. This can be used to set values of any acceptable type into a corresponding index of an array.
it is something like
if (!defined('APPLICATION_PATH')) define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
or, in human's language
defined('APPLICATION_PATH') or define('APPLICATION_PATH', realpath(dirname(_FILE_) . '/../application'));
Your assumption is correct. This is just a short hand way of saying
if (!defined('APPLICATION_PATH')) {
define('APPLICATION_PATH', '...');
}
You can easily test this:
define("foo", "bar");
defined("foo") || define("foo", "baz");
var_dump(foo);
Output is bar
.
//define("foo", "bar");
defined("foo") || define("foo", "baz");
var_dump(foo);
Output is baz
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With