Best Way to Test For a Variable'S Existence in PHP; Isset() Is Clearly Broken

Best way to test for a variable's existence in PHP; isset() is clearly broken

If the variable you are checking would be in the global scope you could do:

array_key_exists('v', $GLOBALS) 

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

How to check $_GET isset for a parameter and value?

You first need to check if the parameter is set and after that if parameter equals the expected value.

Like this:

<?php if (isset($_GET['name'])) : ?>
<?php if ($_GET['name'] === 'john') : ?>
// Name exists and is john
<?php endif; ?>
<?php endif; ?>

How isset() works?

isset($var) checks that the variable is defined in the current scope and its value is not null. Some examples:

<?php 
isset($var); //false
?>

<?php
$var = null;
isset($var); //false
?>

<?php
$var = "some string";
isset($var); //true
?>


<?php
$var = "";
isset($var); //true
?>


<?php
$var = false;
isset($var); //true
?>

How to know if a PHP variable exists, even if its value is NULL?

Use the following:

$a = NULL;
var_dump(true === array_key_exists('a', get_defined_vars()));

Easiest way to check for existence of a value

Its sort of subjective, as there are various ways, and some people find one way easier than another. What you wrote can even be considered easiest by some.

Also, for example, you could just do:

echo (isset($result->data['id'])?$result->data['id']:'');

Or, you could put it in a standard if block:

if (isset($result->data['id'])) { echo $result->data['id']; }

Another way, would be to make a tiny little helper function that floats about that you reuse:

function echoVar(&$input=null) { if (isset($input)) { echo $input; } }

echoVar($result->data['id']);
echoVar($result->data['name']);

I've not really used the mini function method, so I cannot say for sure if its really any easier, since it really obscures your code reading it later.

One thing to note is, isset can take the full nested variable, and it checks in step. You do not need to clal three issets in a row on each level of your intended value.

Alternative to isset(user_input) in php

The equivalent of isset($var) for a function return value is func() === null.

isset basically does a !== null comparison, without throwing an error if the tested variable does not exist. This is a non-issue for function return values, since a) functions must exist (or PHP will exit with a fatal error) and b) a function always returns something, at least null. So all you really need to do is to check for null, no isset necessary.

I've written about this extensively here: The Definitive Guide To PHP's isset And empty.


Beyond this, it depends on what exactly you want to check:

  • test if a key was submitted via GET/POST: isset($_POST['key'])
  • test if there's a value and whether it's not == false: !empty($_POST['key'])
  • test if it's a non-empty string: isset($_POST['key']) && strlen($_POST['key'])
  • perhaps much more complex validations: filter_input

php check if variable isset in the best way

$myvar1 = isset($myvar2) ? $myvar2 : 'lorem';

you need ternary operator

syntax:

(expression) ? whenExpIsTrue : whenExpIsFalse;

it's equal to:

if (isset($myvar2)) {
$myvar1 = $myvar2;
} else {
$myvar1 = 'lorem';
}

or

$myvar1 = 'lorem'; //default value
if (isset($myvar2)) {
$myvar1 = $myvar2;
}


Related Topics



Leave a reply



Submit