Very Basic Question

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
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

Very Basic Question

Post 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?
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Code: Select all

<?php if($var == "") 
{ 
echo "Your name is $var"; 
} 
else 
{ 
echo "You did not enter a name."; 
} 
?>
smoky989
Forum Commoner
Posts: 41
Joined: Mon Sep 02, 2002 1:14 pm

Post by smoky989 »

Sorry, I had it in a PHP tag already. Not working
nieve
Forum Newbie
Posts: 11
Joined: Tue Dec 03, 2002 10:26 pm

Post by nieve »

Code: Select all

<?
if(!$var)  
{
echo "You did not enter a name.";  
} else {  
echo "Your name is $var";  
}  
?>
User avatar
qiangxd
Forum Newbie
Posts: 8
Joined: Thu Dec 05, 2002 6:00 pm

Post by qiangxd »

Code: Select all

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



?>
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

qiangxd and smoky989, Parse error you are missing ";"
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

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