Solved

Controll structures with boolean instead of "0"

Posted on 2003-11-17
6
220 Views
Last Modified: 2011-09-20
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 )

0
Comment
Question by:psimation
  • 4
  • 2
6 Comments
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9763907
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.
0
 
LVL 17

Author Comment

by:psimation
ID: 9765784
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.
0
 
LVL 6

Expert Comment

by:DoppyNL
ID: 9765817
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.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 6

Expert Comment

by:DoppyNL
ID: 9765821
(you don't have to change a thing, everything works fine as it is)
0
 
LVL 17

Author Comment

by:psimation
ID: 9765928
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.
0
 
LVL 6

Accepted Solution

by:
DoppyNL earned 50 total points
ID: 9766194
you would have to use mysql_num_rows($result) to determine the amount of rows that are returned.
and you would have to use error handling to catch any errors while processing the code.

But keep in mind that PHP was written with the knowledge that it can be done this way; so there is no need for that kind of functionality in PHP.
There may be some in PHP though, but that is not intentionally.

If you are looking for ways to code like that you might as well dive into Jave immediatly, as that kind of functionality MUST be in there (otherwise it can't be done).
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now