Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

defined or define in PHP [duplicate]

Tags:

php

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.

like image 861
winteck Avatar asked Oct 11 '11 17:10

winteck


People also ask

What is defined in PHP?

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().

What is the use of => in PHP?

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.


2 Answers

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'));
like image 109
genesis Avatar answered Oct 16 '22 13:10

genesis


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.

like image 40
bismark Avatar answered Oct 16 '22 11:10

bismark