?
Solved

Controll structures with boolean instead of "0"

Posted on 2003-11-17
6
Medium Priority
?
231 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 200 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 6 hours left to enroll

850 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