Re: Newbie Question (and an odd bug)

From: Date: Mon, 23 Apr 2001 18:51:19 +0000
Subject: Re: Newbie Question (and an odd bug)
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-49904@lists.php.net to get a copy of this message
Furthermore, it's also buggy. If the user submits an empty string the if() will consider it to be not equal to "0", which will execute whatever is in the if statement. Not only that, but if the user submits the number 0 it invites bugs because the string "0" is not equal to the number 0. Even if you are the one setting the value of $my_type don't do it. What's more, it's very bad programming practice to quote a number. Only quote strings. You could do: if ($my_type != true) But it's a bad idea because it's so weird. Far easier to read is: if (!$my_type) Easier to type too, and it's nearly bug proof. I say "nearly", because if PHP you want to pass the number zero inside of a string PHP may or may not evaluate it to true. So if you are _really_ serious about seing if the user submitted a field, use: if (!$my_type && !isempty($my_type)) I've never accepted the number 0 as a valid field, so I've never had a problem with it, but it's something you should known. One more thing you'd benefit from knowing. Your original: if (my_type != "0") Isn't bad syntax, but it is bad form to get used to. The better is: if (MY_TYPE) It's a rarely used feature in PHP known as a constant, but it's very useful (on rare occasions, anyway. so rare, actually, that I've never needed to use it). Check the manual for the define() function. To make the above if statement work the MY_TYPE constant must exist. Execute this code: define(MY_TYPE, "Aren't constants neet?"); if (MY_TYPE) { echo "The following is a constant: " . MY_TYPE; } else { echo "The constant MY_TYPE does not exist."; } Neet, no? But here's something that's really weird about PHP. Try commenting out the line with define() on it. Isn't that weird? To my knowledge, a string that appears after echo must be quoted, but in practice PHP handles unquoted strings with echo just fine. Could be a bug, but I doubt it. Furthermore, the above loop is always true, even if MY_TYPE has not been set to a value. Now that should be (and probably is) a bug. But what do I know :) -- Plutarck Should be working on something... ...but forgot what it was. "Geir Eivind Mork" <geir.eivind@mork.as> wrote in message news:0104231749060K.05150@maria... > On Monday 23 April 2001 17:34, Taylor, Stewart wrote: > > You've got some typo's > > if (my_type != "0"){ --> if ($my_type != "0") { > > if ($my_type) > > if it's a check if it's true, "not-equal"-ing it to string 0 is a ugly way to > do it. just my two cents. > -- > php developer / CoreTrek AS | Forest fires cause Smokey Bears. > Sandnes / Rogaland / Norway | > web: http://www.moijk.net/ | > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#49904) next »