Interesting PHP behavior..

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Interesting PHP behavior..

Post 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;
}
Last edited by Benjamin on Fri Jul 14, 2006 5:26 am, edited 1 time in total.
jamiel
Forum Contributor
Posts: 276
Joined: Wed Feb 22, 2006 5:17 am
Location: London, United Kingdom

Post by jamiel »

You sure both values aren't null? That would make it true.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Re: Interesting PHP behavior..

Post 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;
}
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

They're dead convenient sometimes though.

Code: Select all

if ($something) do_something();
When the if and the line are on the same line, everything is hunky dory for me.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Yeah I'd certainly rather see one-liner if statements than see ternary statements.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

Re: Interesting PHP behavior..

Post 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.
Last edited by santosj on Fri Jul 14, 2006 1:12 pm, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
santosj
Forum Contributor
Posts: 157
Joined: Sat Apr 29, 2006 7:06 pm

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