Solved

Controll structures with boolean instead of "0"

Posted on 2003-11-17
6
223 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cpanel file manager 8 42
How to fix Datetime in MySQL? 4 50
PHP Soap Server returning XML but < & > as &lt; / &gt; 9 49
Wordpress Query 5 27
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

828 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