Alex_B
Alex_B

Reputation: 1661

php testing isset() and empty() in a function

I am trying to encapsulate isset() and empty() in a function.

It worked fine on my home development sever (apache 2.4.3, PHP 5.2.17 under WinXP). When I ported it to my university Linux machine running Fedora, I got a notice about undefined index.

I checked my php.ini on my home computer, and error reporting is set to all. I put error_reporting(E_ALL); in my PHP script to try duplicate the error. It didn't happen.

Question 1: Why am I not getting the notice on my home development computer?

Here is my function:

<?php
function there($s) {
    if (! isset($s)) {
        return false;
        }
    if (empty($s)) {
        return false;
        }
    return true;
}
?>

Here I test if a session variable exists:

if (! there($_SESSION['u'])) {
    $_SESSION['u'] = new User();
    }

I switched my function so that I test empty() before I test isset() thinking this will avoid getting the notice. I haven't had a chance yet to test this at school.

Question 2: Does empty() in general avoid giving a notice if the variable is undefined, or not set?

Question 3: Can I use my there() function to do this, or will I get the notice just by passing the undefined or unset parameter?

Upvotes: 2

Views: 1005

Answers (4)

Dom
Dom

Reputation: 2410

I realize this is an old thread, and it already has an accepted answer. So, I will not answer the questions again here. But for completeness sake and in the hope that it may help others I am sharing something I use. Take a look at the following code:

<?php
echo var_dump($_SESSION['something']);

This will give you:

PHP Notice: Undefined variable: _SESSION in - on line 2

NULL

But if you do this:

<?php
echo var_dump(mycheck($_SESSION['something']));

function mycheck( &$var ){ // Note the & so we are passing by reference
    return $var;
}

You will get:

NULL

This is because internally the $var variable will get created the instant the function is called. But since the $_SESSION['something'] does not exist, the $var is getting set to null which is then returned. Voila, notice gone. But be aware that the variable $_SESSION['something'] has now been created internally, even though isset($_SESSION['something']) will still return false because isset determines 'if a variable is set and is not NULL'.

PHP manual for isset: http://php.net/manual/en/function.isset.php

Upvotes: 0

Cups
Cups

Reputation: 6896

As a way of proving @Kolink 's fine reply really, something which I had not realised ...

echo '<form action="" method=POST>
<input type=text value="" name="string" />
<input type=text value=0 name="int" />
<input type=submit />
</form>';

if( empty($_POST['string'] ) ){ 
var_dump( $_POST['string']) ;
}
if( empty($_POST['int'] ) ){ 
var_dump( $_POST['int']) ;
}
if( empty($_POST['something'] ) ){ 
echo 'something was not set, but you cannot var_dump it without a warning';
}

Upvotes: 0

AD7six
AD7six

Reputation: 66388

Your function is almost equivalent to !empty:

if (empty($_SESSION['u'])) {
    $_SESSION['u'] = new User();
}

The only difference is: your wrapper function will complain if it's passed a variable that doesn't exist - whereas, as a language construct empty will not (and neither will isset).

Your sub-questions

Why am I not getting the notice on my home development computer?

Probably because you've got error reporting turned off.

Does empty() in general avoid giving a notice if the variable is undefined, or not set?

Yes.

Can I use my there() function to do this, or will I get the notice just by passing the undefined or unset parameter?

You will get notices about undefined variables.

Upvotes: 2

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324840

isset and empty are not functions, they are language constructs. As such, they can get away with such things as reading the literal variable name instead of trying to access the value. Any function you define can't do that.

With that in mind, there is no need to test for both. empty is a superset of isset, so you only really need to check for empty.

Upvotes: 3

Related Questions