Page 1 of 1
Interesting PHP behavior..
Posted: Fri Jul 14, 2006 5:17 am
by Benjamin
The code below executed and ran without any notices, warnings or errors of any sort. It did make for some interesting behavior though. The if statement evaluated to true every time, regardless of whether or not they matched. Notes added and non relevent code has been removed.
This ran on PHP Version 5.0.4
Code: Select all
switch ($_POST['Action']) {
case 'blah':
break;
case 'blah2':
while ($Data = $Database->fetchRow) {
if ($_POST['FieldName'] == $Data['Field']) // missing an opening {
$Status['Error'] = true;
$Status['Message'] = 'A database field by that name already exists.';
break 2;
}
// missing a closing }
break;
}
Posted: Fri Jul 14, 2006 5:20 am
by jamiel
You sure both values aren't null? That would make it true.
Posted: Fri Jul 14, 2006 5:29 am
by Benjamin
Yeah they were both strings and they didn't match. It also either didn't set $Status['Error'], or it changed it's value to false. $Status['Message'] was set to the error message though.
Re: Interesting PHP behavior..
Posted: Fri Jul 14, 2006 6:22 am
by timvw
astions wrote:behavior though. The if statement evaluated to true every time, regardless of whether or not they matched.
I don't see the problem.. (Apart that i would indent a little different... And perhaps use braces...)
Code: Select all
switch ($_POST['Action']) {
case 'blah':
break;
case 'blah2':
while ($Data = $Database->fetchRow) {
if ($_POST['FieldName'] == $Data['Field']) {
$Status['Error'] = true;
}
$Status['Message'] = 'A database field by that name already exists.';
break 2;
}
break;
}
Posted: Fri Jul 14, 2006 10:00 am
by Ambush Commander
Yep, timvw's got it. When if() doesn't have braces, it just gobbles the immediately following line. These two are equivalent:
Code: Select all
if ($true) {
do_something();
}
if ($true)
do_something();
Personally, I never use the second way.
Posted: Fri Jul 14, 2006 10:02 am
by Benjamin
Yeah I never do either, I was up for a long time and forgot the two braces and things went wacky...
I would lean towards a parse error for if statements with no braces.
Posted: Fri Jul 14, 2006 10:05 am
by Ambush Commander
They're dead convenient sometimes though.
When the if and the line are on the same line, everything is hunky dory for me.
Posted: Fri Jul 14, 2006 10:32 am
by Chris Corbyn
Yeah I'd certainly rather see one-liner if statements than see ternary statements.
Re: Interesting PHP behavior..
Posted: Fri Jul 14, 2006 1:06 pm
by santosj
timvw wrote:
Code: Select all
switch ($_POST['Action']) {
case 'blah':
break;
case 'blah2':
while ($Data = $Database->fetchRow) {
if ($_POST['FieldName'] == $Data['Field']) {
$Status['Error'] = true;
}
$Status['Message'] = 'A database field by that name already exists.';
break 2;
}
break;
}
http://us3.php.net/manual/en/language.o ... arison.php
Code: Select all
if(strcmp($_POST['FieldName'], $Data['Field']) == 0) {
$Status['Error'] = true;
}
$Status['Message'] = 'A database field by that name already exists.';
I wouldn't trust PHP or any language to judge the similarities of Strings based on a single '==' comparsions. I would try the function instead and if it still doesn't work, I will stick a fork in my eye and post the pictures.
I have read that '===' may also work.
Posted: Fri Jul 14, 2006 1:09 pm
by RobertGonzalez
Did you echo the strings within the function to make sure that what you were seeing was what the function was seeing? If they are the same in the database but something goes wonkiy in the app, there is a chance that the function could see if(1 == 1) or something silly like that.
Posted: Fri Jul 14, 2006 1:44 pm
by Christopher
d11wtq wrote:Yeah I'd certainly rather see one-liner if statements than see ternary statements.
I'm just the opposite. I use ternary statements for one liners because then I know it is a one liner. And I can then always put braces on if()s.
Posted: Fri Jul 14, 2006 1:47 pm
by Ambush Commander
The problem with ternaries is that you're still left with that "colon something", even when else isn't required.
As for string comparisons, PHP will only get wonky on you if it has to type juggle. Otherwise, things should work hunky dory.
Posted: Fri Jul 14, 2006 2:37 pm
by santosj
Ambush Commander wrote:The problem with ternaries is that you're still left with that "colon something", even when else isn't required.
As for string comparisons, PHP will only get wonky on you if it has to type juggle. Otherwise, things should work hunky dory.
True, but what if? It is the type juggling that could be the problem.