FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Setting variable to false in a WHILE loop
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
Setting variable to false in a WHILE loop [message #175434] Sat, 24 September 2011 03:53 Go to next message
jwcarlton is currently offline  jwcarlton
Messages: 76
Registered: December 2010
Karma: 0
Member
I'm doing a MySQL query, then running a WHILE loop on it, like so:

while (list($id, $title, $text) = mysql_fetch_row($sth)) {
if ($text) {
// do whatever
}
}

The problem that I'm having is that sometimes, it's possible for $text
to equal "" instead of NULL. I use it as a quick way to check whether
the row is new (in which case $text is NULL), or if it's been updated
(in which case $text is ""), without requiring an additional field.

I know that I could just say:

if ($text && ($text != "")) {

And I could move the list() inside of the loop, like so:

while ($row = mysql_fetch_row($sth) {
$text = false;
list($id, $title, $text) = $row;

if ($text) {
// do whatever
}
}


But I'm curious if there's a way to reset $id, $title, and $text to
false at the beginning of the loop. Something like:

// Just a mock-up, using "my" from Perl
while (my(list($id, $title, $text)) = mysql_fetch_row($sth)) {
Re: Setting variable to false in a WHILE loop [message #175436 is a reply to message #175434] Sat, 24 September 2011 13:14 Go to previous messageGo to next message
Jerry Stuckle is currently offline  Jerry Stuckle
Messages: 2598
Registered: September 2010
Karma: 0
Senior Member
On 9/23/2011 11:53 PM, jwcarlton wrote:
> I'm doing a MySQL query, then running a WHILE loop on it, like so:
>
> while (list($id, $title, $text) = mysql_fetch_row($sth)) {
> if ($text) {
> // do whatever
> }
> }
>
> The problem that I'm having is that sometimes, it's possible for $text
> to equal "" instead of NULL. I use it as a quick way to check whether
> the row is new (in which case $text is NULL), or if it's been updated
> (in which case $text is ""), without requiring an additional field.
>
> I know that I could just say:
>
> if ($text&& ($text != "")) {
>
> And I could move the list() inside of the loop, like so:
>
> while ($row = mysql_fetch_row($sth) {
> $text = false;
> list($id, $title, $text) = $row;
>
> if ($text) {
> // do whatever
> }
> }
>
>
> But I'm curious if there's a way to reset $id, $title, and $text to
> false at the beginning of the loop. Something like:
>
> // Just a mock-up, using "my" from Perl
> while (my(list($id, $title, $text)) = mysql_fetch_row($sth)) {
>

Why complicate things? Why not just use

if ($text !== false)


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
Re: Setting variable to false in a WHILE loop [message #175453 is a reply to message #175436] Wed, 28 September 2011 18:28 Go to previous messageGo to next message
tamouse is currently offline  tamouse
Messages: 5
Registered: September 2011
Karma: 0
Junior Member
On Saturday, September 24, 2011 8:14:46 AM UTC-5, Jerry Stuckle wrote:
> Why complicate things? Why not just use
>
> if ($text !== false)

That works, but might be a little opaque. I'd use:

if (!empty($text))

As it's a little more clear to the reader/maintainer.

Also, with the === & !== idiom, isn't it better reverse the operands, thus:

if (FALSE !== $text)

?
Re: Setting variable to false in a WHILE loop [message #175458 is a reply to message #175453] Wed, 28 September 2011 19:49 Go to previous messageGo to next message
Tim Streater is currently offline  Tim Streater
Messages: 328
Registered: September 2010
Karma: 0
Senior Member
In article
<9281403.1198.1317234500379.JavaMail.geo-discussion-forums@yqma37>,
tamouse <tamouse(dot)lists(at)gmail(dot)com> wrote:

> On Saturday, September 24, 2011 8:14:46 AM UTC-5, Jerry Stuckle wrote:
>> Why complicate things? Why not just use
>>
>> if ($text !== false)
>
> That works, but might be a little opaque. I'd use:
>
> if (!empty($text))
>
> As it's a little more clear to the reader/maintainer.
>
> Also, with the === & !== idiom, isn't it better reverse the operands, thus:
>
> if (FALSE !== $text)

This business of doing:

if (27===$myvar) ...

is the dopiest thing I ever heard of. 27 is equal to 27, end of. People
should really learn that the computer serves *them*, not the other way
around.

--
Tim

"That excessive bail ought not to be required, nor excessive fines imposed,
nor cruel and unusual punishments inflicted" -- Bill of Rights 1689
Re: Setting variable to false in a WHILE loop [message #175460 is a reply to message #175453] Thu, 29 September 2011 12:09 Go to previous messageGo to next message
Doug Miller is currently offline  Doug Miller
Messages: 171
Registered: August 2011
Karma: 0
Senior Member
On 9/28/2011 2:28 PM, tamouse wrote:

> Also, with the ===& !== idiom, isn't it better reverse the operands, thus:
>
> if (FALSE !== $text)
>
Why would you think that such a grossly counterintuitive construct is
better than the clear and obvious
if ($text !== FALSE)
?
Re: Setting variable to false in a WHILE loop [message #175478 is a reply to message #175453] Sat, 01 October 2011 14:08 Go to previous message
Thomas 'PointedEars'  is currently offline  Thomas 'PointedEars'
Messages: 701
Registered: October 2010
Karma: 0
Senior Member
tamouse wrote:

> On Saturday, September 24, 2011 8:14:46 AM UTC-5, Jerry Stuckle wrote:
>> Why complicate things? Why not just use
>>
>> if ($text !== false)
>
> That works, but might be a little opaque. I'd use:
>
> if (!empty($text))
>
> As it's a little more clear to the reader/maintainer.

It is not equivalent, though:

<http://php.net/empty>

> Also, with the === & !== idiom, isn't it better reverse the operands,
> thus:
>
> if (FALSE !== $text)
>
> ?

No. Why should it be?

(The PEAR Coding Standards also actively recommend against writing `false'
as `FALSE': <http://pear.php.net/manual/en/standards.naming.php>)


PointedEars
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Contact Form
Next Topic: OCI.DLL and libcs.dll?
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Sat Nov 30 23:13:56 GMT 2024

Total time taken to generate the page: 0.01820 seconds