Reputation: 6471
I want to echo Name:
if the row name
in my SQL table is not empty.
I am a little bit confused, because something else happens then what I am expecting. This is my code:
if ($row['name'] != 0 ) echo 'Name: ';
echo($row['name']);
So if the name in my SQL row is Fred, it should print Name:Fred
, and if the row in my SQL table is empty it should print out nothing. Print out nothing is working fine, but when the name in my row is Fred
it prints out only Fred
. But if the name in my row is 1Fred
it prints out Name:1Fred
. So my code is only working, when a number is inside the name...
Upvotes: 0
Views: 441
Reputation: 2135
Always use brackets, no matter what you see in other developers code. For testing whether a variable is empty or not, use the empty()
command.
if (empty($row['name']) == true) {
echo 'This is empty';
} else {
echo 'This is not empty';
}
EDIT: There is this comparison table that shows the difference between empty()
, null
and isset()
: Comparison
Upvotes: 1
Reputation: 1216
Try it like this:
if ($row['name'] != 0 ) {
echo 'Name: ';
echo($row['name']);
}
What did you do wrong:
After a condition you can either have one statement (in your case: echo 'Name: ';
, or you have a list of statements that then need to be enclosed in curly bracers.
Otherwise, your computer ahs no way of knowing when the conditional statements end.
Upvotes: 0