Page 1 of 1

Very Basic Question

Posted: Fri Jan 10, 2003 8:45 pm
by smoky989
ok I'm trying to do something I thought was simple but it won't work. I just have a simple control structure and if the variable is blank I want one thing to happen if its not I want another to happen. Heres an example of my code.

if($var == "")
{
echo "Your name is $var";
}
else
{
echo "You did not enter a name."
}

Any ideas what the problem is?

Posted: Fri Jan 10, 2003 9:11 pm
by Kriek

Code: Select all

<?php if($var == "") 
{ 
echo "Your name is $var"; 
} 
else 
{ 
echo "You did not enter a name."; 
} 
?>

Posted: Fri Jan 10, 2003 9:15 pm
by smoky989
Sorry, I had it in a PHP tag already. Not working

Posted: Fri Jan 10, 2003 9:26 pm
by nieve

Code: Select all

<?
if(!$var)  
{
echo "You did not enter a name.";  
} else {  
echo "Your name is $var";  
}  
?>

Posted: Fri Jan 10, 2003 9:27 pm
by qiangxd

Code: Select all

<?php
if($var != "") 
{ 
echo "Your name is $var"; 
} 
else 
{ 
echo "You did not enter a name." 
} 



?>

Posted: Fri Jan 10, 2003 9:34 pm
by Kriek
qiangxd and smoky989, Parse error you are missing ";"

Posted: Mon Jan 13, 2003 3:23 am
by twigletmac
To check if a variable is empty (that is is either equal to an empty string or 0) you can use the empty() function:

Code: Select all

if(!empty($var)) { 
    echo 'Your name is '.$var; 
} else { 
    echo 'You did not enter a name.'; 
}
Mac