Continuous PHP testing undefined

In PHP, if I define a constant like this:

define('FOO', true);
if(FOO) do_something();

The method do_somethingexecutes as expected.

But if I do not define a constant BOObelow:

if(BOO) do_something();

Then executed as well do_something. What's going on here?

+6
source share
7 answers

BOOwill be forced into a string BOOthat is not empty, so it is true.

Codepad .

This is why some people who do not know the best access to an array element with $something[a].

You must enter a code error_reporting(E_ALL)that will then give you ...

: undefined constant HELLO - "HELLO" /t.php 5

, defined(). , PHP , , ...

<?php defined('APP') OR die('No direct access');

- , .

+10

, :

PHP: undefined BOO - "BOO" N

, , PHP , "BOO" . , '' '0', "", .

+4

, , , , : if(BOO === true) if(BOO === false)

+3

PHP , , true.

:

bool defined ( string $name )

, :

if(defined('BOO')) {\\code }
+2
if($FOO) do_something(); 

FOO , , . PHP defined.

+2

PHP . , , :

function consttrue($const) {
    return !defined($const) ? false : constant($const);
}
+2

- php constant(), :

if (constant('BOO')) doSomething();

.

PHP-, .

Ap php docs, , ; null.

null falsey, , .
, , - true (, , ) , Falsey. , , .

if (constant('IS_DEV')) {
  // *Remember to enclose the constant name in quotes.*
  // do stuff that should only happen in a dev environment
  // By Default, if it didn't get defined it is, as though, 'false'
}

constant() . , php , ( ) .
, PHP , .

, :

if (defined('IS_DEV') && (IS_DEV)) {
  // *Remember to enclose the constant name in quotes for the FIRST operator.*
  // do stuff that should only happen in a dev environment
}

, , - === !==, ( ), .

if (IS_DEV === true)) {
  // do stuff that should only happen in a dev environment
}
0

All Articles