think tank forum

technology » answer a php question!

lucas's avatar
15 years ago
link
lucas
i ❤ demo
why does this script produce two fatal errors?

<?php

$string = "     ";

echo "str is '$string'\n";
echo "empty(str) is '".empty($string)."'\n";
echo "trim(str) is '".trim($string)."'\n";
echo "isset(trim(str)) is '".isset(trim($string))."'\n";
echo "empty(trim(str)) is '".empty(trim($string))."'\n";

?>
lucas's avatar
15 years ago
link
lucas
i ❤ demo
i get it. isset() and empty() must be fed variable names, not variables faces.

i guess php is as idealistic as i am. /o/
 
15 years ago
link
Mathew
Zombie Flesh Eaters
What are you talking about faces vs. names?
lucas's avatar
15 years ago
link
lucas
i ❤ demo
you have to use the name of a variable, not the face-value of it.

<?php

$str = "abc";

echo(empty($str)); // this will work.

echo(empty("$str")); // this will NOT work.

?>
Carpetsmoker's avatar
15 years ago
link
Carpetsmoker
Martin
empty("$str") passes a string to empty(), it is the same as empty('hello'), since empty() and isset() work on the variables themselves, and not on the values (i.e. strings), so it produces an error.
lucas's avatar
15 years ago
r1, link
lucas
i ❤ demo
or: names, not faces. :D
Carpetsmoker's avatar
15 years ago
link
Carpetsmoker
Martin
Your terminology is confusing ...
Étrangère's avatar
15 years ago
link
Étrangère
I am not a robot...
Duh
ozntz's avatar
15 years ago
link
ozntz
toooooooooooooooooooooooooooooooooo
4 realz
 
15 years ago
link
Antitussif
hijacked
oh lolz
 
15 years ago
link
Mathew
Zombie Flesh Eaters
You're passing a literal string rather than the variable (which in this case is by value, not reference). Nub