0

I have a field that depends has custom text if a certain condition is met...and if it isn't the field is blank.

I have written a custom function test if a variable is set

function AmISet($fieldName) {

if (isset($fieldName)) {

    echo "I am set";
}else{
    echo "I am not set";    
    }
};

but when I attach it the field I get an error that the variable is undefined. But when I do a regular isset($fieldName); I don't have a problem. Is there any way to get around this and have my function do this instead of the isset()?

I want to add other logic in the function but I only want it to work if the variable is set...but I don't want the undefined error if it is not.

I am new to php and really appreciate any help or direction you can give me. Thank you for the help!

René Höhle
  • 26,716
  • 22
  • 73
  • 82
  • `isset()` is not a function but a language construct. There's no way to write a function that accepts non-existing variables without complaint. Perhaps if you elaborate on where `$fieldName` comes from and why it can be empty we could think of a solution. – Álvaro González Apr 28 '14 at 15:19
  • @ÁlvaroG.Vicario the $fieldName comes from a query that is performed when a checkbox is checked. If the box is checked the query is made and the variable is set from the query, if not it doesn't exist and the field is blank. So sometimes a user could have field values and other times they wouldn't exist. – greyglassbottle Apr 28 '14 at 15:27

2 Answers2

5

You need to pass the variable by reference:

function AmISet(&$fieldName) {
    if (isset($fieldName)) {
        echo "I am set\n";
    } else {
        echo "I am not set\n";    
    }
}

Test cases:

$fieldName = 'foo';
AmISet($fieldName); // I am set

unset($fieldName);
AmISet($fieldName); // I am not set

However, this function is not useful as it is, because it will only output a string. You can create a function that accepts a variable and return if it exists (from this post):

function issetor(&$var, $default = false) {
    return isset($var) ? $var : $default;
}

Now it can be used like so:

echo issetor($fieldName); // If $fieldName exists, it will be printed
Community
  • 1
  • 1
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 1
    Yes! Thank you so much, this is what I have been looking for! – greyglassbottle Apr 28 '14 at 15:33
  • @DanFromGermany What you mean of "accept the first answer", here I don't think it is right to say to any user like this to accept your answer when OP accept the answer of another user. – Ajay S Apr 28 '14 at 16:07
  • *What am I gonna do 2moz? Copy some answers right after they got posted, change a little bit and hopefully earn the rep! That gonna be hella fun!* – Daniel W. Apr 28 '14 at 16:12
  • 1
    issetor() function doesn't work on nested objects. – Muhammed Mar 12 '21 at 03:12
1

the $fieldName comes from a query that is performed when a checkbox is checked. If the box is checked the query is made and the variable is set from the query, if not it doesn't exist and the field is blank.

Filter functions are designed for this kind of tasks:

<input type="foo" value="1">
$foo = filter_input(INPUT_POST, 'foo')==='1';

(There's also a specific FILTER_VALIDATE_BOOLEAN filter you can pass as second argument.)

And now you have a pure PHP boolean that always exists.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Thank you for your help. This looks very promising. I will have to rewrite my page now to include this and then I don't have to deal with the undefined variable problem. – greyglassbottle Apr 28 '14 at 16:05