Page 1 of 1

Setting default value: $var = (isset($var)) ? $_POST['var']

Posted: Mon Sep 10, 2007 5:12 pm
by mikebr
I am using a form script and want to check for the value of $val, it can be set either from a config file or from a hidden field in a form...

1# First I want to check the value doesn't exist or has been set in the config file
2# if not then check if it has been set in the form
3# then if not set a default value for $var

So to cut down on the if, elseif's I tried to do it as follows:

Code: Select all

$var = (isset($var)) ? $_POST['var'] : 'DEFAULT_VALUE';
unfortunately if the value $var is set the isset($var) in the above line doesn't seem to read that it has a value, nor does POST and it just sets it to 'DEFAULT_VALUE'

anyone any idea?

Thanks

Posted: Mon Sep 10, 2007 5:15 pm
by Luke
You forgot to check if that variable was set in $_POST

Code: Select all

if (!isset($var))
{
    $var = isset($_POST['var']) ? $_POST['var'] : 'DEFAULT_VALUE';
}

Posted: Mon Sep 10, 2007 5:47 pm
by mikebr
Yep, that's seems to do the trick.

Thanks