Link to home
Start Free TrialLog in
Avatar of psimation
psimationFlag for South Africa

asked on

Controll structures with boolean instead of "0"

Hi there.

I think most of us are familiar with this type of coding in PHP:

...
$sql = "select * from table";
$result = mysql_query($sql);

while ($myrow = mysql_fetch_assoc($result)) {
 extract($myrow);
 echo $field1.' :: '.$field2.' :: '.$field3.'<br>';
}
...

Now, if PHP did not consider "0" to be equivalent to "false", how would I write this code snippet?
( I have my own ideas, but would much rather get some expert opinions )

Avatar of DoppyNL
DoppyNL

errrmmm

what do you mean?

You want to check if some variable is equivalent to false ? but not return it true when it is 0; but only when it is really false??

do:

if ($boolean_variable === false)

otherwise, be a bit more specific, I didn't quite get it.
Avatar of psimation

ASKER

I thought i was rather specific with my code sample, but let me try again:

If php did not accept "0" as the same as a boolean value (false), and anything larger than 0 as "true", then we would generally have to write our if's and whiles etc as

if ( x == 0) { //this would then only happen if x = 0
}

instead of just

if (!x) {
}

What I'm asking is for somebody to please re-write in as little code as possible my example while statement as per my original question.

PHP's acceptance as 0 = false, allows one to do more that just cut a few coreners, it also allows me to use a the return from a "command" as a "test" for the if/while

In my mind, I cannot do this if the loop only accepted boolean.,.

So, I'm trying to get my mind around how I would have written the exact thing but this time, checking for boolean instead of checking for 0 or > 0 while dealing with the assignment of the new row each time in the loop.
To come to your code:
-----------
$sql = "select * from table";
$result = mysql_query($sql);

while ($myrow = mysql_fetch_assoc($result)) {
 extract($myrow);
 echo $field1.' :: '.$field2.' :: '.$field3.'<br>';
}
------------
We are talking here about the value wich mysql_fetch_assoc() returns.
it will return false if $result is not a valid result set (and an error message)
it will return false if $result contains a result set, but no records are left to return
it will return a value (wich will be evaluated as true in an expression) when records are left; the value is actually a record.

Conclusion:
the while loop will only be executed when a record is returned by mysql_fetch_assoc
This is normal coding practice in PHP and the manual also says you should do it like this.
(you don't have to change a thing, everything works fine as it is)
Hi DoppyNL
I think you are missunderstanding my question.

I know the code will work fine as it is. I am just curious as to how we would have coded it if things were not as they are.
 I am currently learning Java, and for one, Java does NOT accept "0" as false and >0 as true, you HAVE to have a statement returning a boolean type as the argument to your if/while statements.

Seeing that I have only worked with PHP, and have grown accustomed to the "nice" shortcut way that PHP allows, I was hoping that someone here could help me to think along that line.

I know full well all the PHP rules of when a while will execute and when it will not, what I am not sure of is how to write this code when I am forced to look for true/false instead of 0 or >0. $myrow = mysql_fetch_assoc($result) will NOT return true or false but 0 or >0. So I would have to have a couple of intermediary assignments in the above snippet in order for the while to execute ( in a boolean world), and I would also have to deal with calling the same statement before the next loop of the while in order to advance the internal pointer of the result set...

I hope you understand now. Sorry if I was not clear enough, it's a weakness I tend to exhibit often.
ASKER CERTIFIED SOLUTION
Avatar of DoppyNL
DoppyNL

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial