Solved

Controll structures with boolean instead of "0"

Posted on 2003-11-17
6
221 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
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 …

910 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

20 Experts available now in Live!

Get 1:1 Help Now