Page 1 of 1

if-variable isnt defined?

Posted: Tue Jun 25, 2002 8:10 pm
by Patriot
is there a 'if' function something like
if $variable=("")
include ("blah.php")

to show a certain page or echo something if a variable isn't defined

Posted: Tue Jun 25, 2002 8:24 pm
by lc
try

if (empty($variable)){
include("blah.php")
}

that's how I do it.

Posted: Wed Jun 26, 2002 1:55 am
by twigletmac
You can use either empty() (as lc said) or isset() the difference between them being that a variable will be considered 'empty' if it is,

Code: Select all

$var = '';
// or
$var = 0;
but a variable will be considered 'set' if it has any value including an empty string or 0.

So to use empty() you would use something like lc's example and to use isset() you would do something like:

Code: Select all

if (!isset($variable)) {
    include 'blah.php';
}
In looking for an undefined variable I personally would use isset() but you can pick whichever you feel is more appropriate.

Mac