if-variable isnt defined?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Patriot
Forum Commoner
Posts: 34
Joined: Tue Jun 18, 2002 1:36 pm

if-variable isnt defined?

Post 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
lc
Forum Contributor
Posts: 188
Joined: Tue Apr 23, 2002 6:45 pm
Location: Netherlands

Post by lc »

try

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

that's how I do it.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply