Checking if a constant is empty

Asked
Active3 hr before
Viewed126 times

7 Answers

constantcheckingempty
90%

I'm pretty sure the point of a constant is knowing the value is predictable - i.e. it being constant - so why would you even -need- to test for empty? I'm insanely curious. =) – J. Steen May 31 '11 at 12:03 , 1 @jim yup - if empty() is really what you want? It would return true when the constant's value is "0" for example. Maybe you want to test for the existence of the constant using defined() instead? – Pekka May 31 '11 at 12:03 , Well, I'm assigning a string to the constant and then using it to show a message to the user. I wanted to test if the constant was empty then show no message. How would you handle that? – jim May 31 '11 at 12:05 ,empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim($name)).

Just letting you know that you can do

if (!empty(MY_CONST))
load more v
88%

Maybe you need to test for the existence of the constant using defined() instead?,I just wanted to make it a little cleaner by just referencing the constant itself.,So it is better to use onblur event or on submitting the form.,onchange will work only if the value of the textbox changed compared to the value it had before, so for the first time it won't work because the state didn't change.

Why is this not possible?

if (!empty(_MY_CONST)) {
   ...

But yet this is:

$my_const = _MY_CONST;
if (!empty($my_const)) {
   ...

define('QUOTA_MSG', ''); // There is currently no message to show

$message = QUOTA_MSG;
if (!empty($message)) {
   echo $message;
}
load more v
72%

Returns the value of the constant, or null if the constant is not defined. , constant() is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function. ,constant — Returns the value of a constant, Return the value of the constant indicated by name.

load more v
65%

To check if constant is defined use the defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is null or false the function will still return true.,To get only those constants that were defined by your app call the function at the beginning and at the end of your script (normally after the bootstrap process):,To get all defined constants including those created by PHP use the get_defined_constants function:,Note that constant becomes "visible" in your code only after the line where you have defined it:

To check if constant is defined use the defined function. Note that this function doesn't care about constant's value, it only cares if the constant exists or not. Even if the value of the constant is null or false the function will still return true.

< ? php

define("GOOD", false);

if (defined("GOOD")) {
   print "GOOD is defined"; // prints "GOOD is defined"

   if (GOOD) {
      print "GOOD is true"; // does not print anything, since GOOD is false
   }
}

if (!defined("AWESOME")) {
   define("AWESOME", true); // awesome was not defined. Now we have defined it 
}
load more v
75%

Running this example on Windows PowerShell 5.1 and PowerShell 6.0 gives you different results.,$null values impact your code differently depending on where they show up.,If you treat the empty $null like a collection, then it's empty.,The primary place you see the difference is when using the pipeline. You can pipe a $null value but not an empty $null value.

Anytime you try to use a variable that you have not initialized, the value is $null. This is one of the most common ways that $null values sneak into your code.

PS > $null - eq $undefinedVariable
True
load more v
40%

A null character (“\0”) is not same as the PHP null constant.,If you pass multiple variables in isset() then all of those variables need to exist to get “true” in return. If one of them doesn’t exist, it will return false. ,isset will return false if you check against a variable with null value.A null character (“\0”) is not same as the PHP null constant.,If you evaluate the variable above with isset(), it’ll return true.

$a = "test";
$b = "anothertest";

var_dump(isset($a)); // TRUE
var_dump(isset($a, $b)); // TRUE

unset($a);

var_dump(isset($a)); // FALSE
var_dump(isset($a, $b)); // FALSE

$foo = NULL;
var_dump(isset($foo)); // FALSE
load more v
22%

if (jQuery('#something').val().length != '') {}
or
if (jQuery('#something').val().length != 0) {}
load more v

Other "constant-checking" queries related to "Checking if a constant is empty"