Weak Typing in PHP: Why Use Isset at All

Weak typing in PHP: why use isset at all?

I want to point out that everyone's reponse I've read here should have one caveat added:

"isset() will return FALSE if testing a variable that has been set to NULL" (php.net/isset).

This means that in some cases, like checking for a GET or POST parameter, using isset() is enough to tell if the variable is set (because it will either be a string, or it won't be set). However, in cases where NULL is a possible value for a variable, which is fairly common when you get into objects and more complex applications, isset() leaves you high and dry.

For example (tested with PHP 5.2.6 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 17 2008 09:05:31)):

<?php
$a = '';
$b = NULL;
var_dump(isset($a));
var_dump(isset($b));
var_dump(isset($c));

outputs:

bool(true)
bool(false)
bool(false)

Thanks, PHP!

Is there any reason to use isset()?

Reason

The reason is, isset() will return boolean and doesn't throw a warning when you check for the existence of variable and proceed. Also, there is a possibility that, a variable may have zero values:

  1. false
  2. 0
  3. ""

But they will be already set.


Example

$varb = false;
$vari = 0;
$vars = "";

isset($varb) // true
isset($vari) // true
isset($vars) // true

if ($varb) // false
if ($vari) // false
if ($vars) // false

PHP: use isset in ternary or not?

As said in the comments, with your if test, php procedes to a type inference.

So there are values in $var that will echo bad even if they're not null.

For a more precise answer, you should check PHP type comparison tables : you have exemples of the differences between if($x), isset(), is_null() and empty() depending on the value of the variable you test.

In where shall I use isset() and !empty()

isset vs. !empty

FTA:

"isset() checks if a variable has a
value including (False, 0 or empty
string), but not NULL. Returns TRUE
if var exists; FALSE otherwise.

On the other hand the empty() function
checks if the variable has an empty
value empty string, 0, NULL or
False. Returns FALSE if var has a
non-empty and non-zero value."

Are these PHP conditionals the same or does one have an advantage

I agree with Sean but I'll outline what each does in plain English:

if ($variable != NULL) {

$variable will be NULL if it hasn't been set. This is practically the same as isset and the same as the variable being undefined.

if (!empty($variable)) {

Generally, this checks whether $variable as a string ((string) $variable) has a strlen of 0. However true will make it return false, as will integers that aren't 0 and empty arrays. For some reason (which I believe to be wrong) $variable = '0'; will return true.

if ($variable) {

This true/false check acts like (boolean) $variable - basically whether the variable returns true when converted to a boolean.

One way to think about it is that it acts the same as empty, except returns the opposite value.

For more information on what I mean by (boolean) $variable (type casting/juggling) see this manual page.

(PHP devs: this is mainly by memory, if I'm wrong here please correct me!)

php isset do you need it in a form check?

if($_REQUEST["input_name"])

will throw a notice (error) if "input_name" doesn't exist, so isset() is recommended.

php isset() function not working on form validation

That's because you are using wrong function here.

isset() checks if variable is set and is not null but when you send form with empty field you'll get empty string. And isset() will return true in such case.

You have to use empty() and just check string length to make sure that something is there.

PHP tutorial, issues with Undefined index and recommended isset fixes

You have to check isset before you assign it to variable. change your php script as below:

<?php

if(isset($_GET['name']) && isset($_GET['age']) && !empty($_GET['name']) && !empty($_GET['age'])) {
$name = $_GET['name'];
$age = $_GET['age'];
echo 'I am '.$name.' and I am '.$age.' years of age.';
} else {
echo 'Please type something.';
}

?>


Related Topics



Leave a reply



Submit