Check If a Variable Is Undefined in PHP

Check if a variable is undefined in PHP

You can use -

$isTouch = isset($variable);

It will return true if the $variable is defined. If the variable is not defined it will return false.

Note: It returns TRUE if the variable exists and has a value other than NULL, FALSE otherwise.

If you want to check for false, 0, etc., you can then use empty() -

$isTouch = empty($variable);

empty() works for -

  • "" (an empty string)
  • 0 (0 as an integer)
  • 0.0 (0 as a float)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • $var; (a variable declared, but without a value)

Check if variable is_undefined in PHP

I haven´t used it yet - but I think that "get_defined_vars" should be worth a look...
http://php.net/manual/en/function.get-defined-vars.php

I would give it a try and dump the result.

What's the best way of checking if an object property in php is undefined?

You can use is_null
Or
!isset($object)

Example :

I want to check if the input is undefined. So, I can show errors.

if (!isset($_POST['myInput'])) { echo "error"; } else { // do the code }

How can I determine if a variable is 'undefined' or 'null'?

You can use the qualities of the abstract equality operator to do this:

if (variable == null){
// your code here.
}

Because null == undefined is true, the above code will catch both null and undefined.

How to distinguish if a PHP property is undefined or set to NULL

PHP has not the value undefined like javascript. But it is not strict typed so if you do not find a better solution here is one with an custom type UNDEFINED

<?php
class UNDEFINED { }

class Test {
var $a;

function __construct( $a='' ) {
$this->a = new UNDEFINED();
if( $a !== '' ) {
$this->a = $a;
}
}

function isDefined() {
$result =true;
if(gettype($this->a) === 'object'){
if(get_class($this->a) === 'UNDEFINED') {
$result=false;
}
}

echo gettype($this->a) . get_class($this->a);
return $result;
}

}

$test= new Test();

$test->isDefined();

Here is a may be litte better version which used instanceof instead of get_call and getType

<?php
class UNDEFINED { }

class Test {
var $id;
var $a;
var $b;

function __construct( $id) {
$this->id = $id;
$this->a = new UNDEFINED();
$this->b = new UNDEFINED();
}

function init( $a = '' , $b = '') {
$this->a = $this->setValue($a,$this->a);
$this->b = $this->setValue($b,$this->b);
}

function setValue($a,$default) {
return $a === '' ? $default : $a;
}

function isUndefined($a) {
return $a instanceof UNDEFINED;
}

public function isFullyLoaded()
{
$result = true;
$properties = get_object_vars($this);
print_r($properties);
foreach ($properties as $property){
$result = $result && !$this->isUndefined($property);
if ( !$result) break;
}
return $result;
}

function printStatus() {
if($this->isFullyLoaded() ) {
echo 'Loaded!';
} else {
echo 'Not loaded';
}
}
}

$test= new Test(1);
$test->printStatus();
$test->init('hello');
$test->printStatus();
$test->init('', null);
$test->printStatus();

isset() vs check if exists for a variable in PHP

$_GET[''] is usually retrieved from the browser, so if you had something like the following:
www.hello.com/index.php?var=hello

You could use:

if(isset($_GET['var'])) {
echo $_GET['var']; // would print out hello
}

Using isset() will stop the undefined index error, so if you just had www.hello.com/index.php for example then you would not see the error even though the var is not set.

Posts are usually when one page posts information to another, the method is the same but using $_POST[''] instead of $_GET['']. Example you have a form posting information:

<form method="post" action="anotherpage.php">
<label></label>
<input type="text" name="text">
</form>

In anotherpage.php to get the information would be something like:

$text = isset($_POST['text']);
echo $text; // would echo what ever you input into the text field on the other page

In a nut shell, just putting $_GET['name'] if name is not set you will get an error.
Using isset($_GET['name']) will check is the var name has any value before continuing.

Not sure if this is what you were after, but from the question this is my best guess at an answer

Php: check if variable value exist as a variable || make variable value a variable

You can use variable variables in PHP, and then check if such a variable has a value. For instance:

$variableA = 'Hello';
$variableB = 'variableA';
echo ${$variableB};

returns Hello on your screen. You can also check if $variableA has a value by doing:

if (isset(${$variableB})) {
....
}

Note that in your question you have variables that have no value, they are not set. The whole purpose of variables is to have a value, hence their name, so your variables are some kind of zombies, not really alive, not really dead. They are not set, so isset() will return false.

How to tell whether a variable is null or undefined in php

Essentially the answer is no. There is not a single function you can create that will tell whether a runtime variable is null or is undefined. (by 'runtime variable' I mean a variable who's name you don't yet know at the time of coding. See Elaboration in the question above).

Relevant Observations:

  • There's no way to retrieve the name of a variable at runtime without giving it a value and hence declaring it.
  • If you pass a variable by reference, instead of by value, you're automatically declaring it. So then in the function you can't go back and determine whether it was declared before you passed it.
  • You can use array_key_exists('variable_name', $GLOBALS) as @zzzzBov stated, to see if a variable has been declared, but only if you know the name of the variable at coding time.

Possible 'Dirty' Solutions

  • As @Phil (and @zzzzBov) explained you could use a messy trick of capturing error messages that would get thrown when you reference an undeclared variable.

  • I also considered a method where you: Make note of all the keys in $GLOBALS, then store a unique value in your target variable (recording it's original value first for later use). And then search $GLOBALS looking for that unique value to determine the name of the variable AND (by comparing with your earlier look at $GLOBALS) determine if the variable existed before. But this also seems messy and unreliable.



Related Topics



Leave a reply



Submit