Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #3198 > unrolled thread
| Started by | jwcarlton <jwcarlton@gmail.com> |
|---|---|
| First post | 2011-09-23 20:53 -0700 |
| Last post | 2011-10-01 16:08 +0200 |
| Articles | 6 — 6 participants |
Back to article view | Back to comp.lang.php
Setting variable to false in a WHILE loop jwcarlton <jwcarlton@gmail.com> - 2011-09-23 20:53 -0700
Re: Setting variable to false in a WHILE loop Jerry Stuckle <jstucklex@attglobal.net> - 2011-09-24 09:14 -0400
Re: Setting variable to false in a WHILE loop tamouse <tamouse.lists@gmail.com> - 2011-09-28 11:28 -0700
Re: Setting variable to false in a WHILE loop Tim Streater <timstreater@greenbee.net> - 2011-09-28 20:49 +0100
Re: Setting variable to false in a WHILE loop Doug Miller <doug_at_milmacdotcom@example.com> - 2011-09-29 08:09 -0400
Re: Setting variable to false in a WHILE loop Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2011-10-01 16:08 +0200
| From | jwcarlton <jwcarlton@gmail.com> |
|---|---|
| Date | 2011-09-23 20:53 -0700 |
| Subject | Setting variable to false in a WHILE loop |
| Message-ID | <a1536ef2-b1a4-473c-a6e8-93d1a7c800e2@k6g2000yql.googlegroups.com> |
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)) {
[toc] | [next] | [standalone]
| From | Jerry Stuckle <jstucklex@attglobal.net> |
|---|---|
| Date | 2011-09-24 09:14 -0400 |
| Message-ID | <j5kl4c$mbs$1@dont-email.me> |
| In reply to | #3198 |
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@attglobal.net
==================
[toc] | [prev] | [next] | [standalone]
| From | tamouse <tamouse.lists@gmail.com> |
|---|---|
| Date | 2011-09-28 11:28 -0700 |
| Message-ID | <9281403.1198.1317234500379.JavaMail.geo-discussion-forums@yqma37> |
| In reply to | #3200 |
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) ?
[toc] | [prev] | [next] | [standalone]
| From | Tim Streater <timstreater@greenbee.net> |
|---|---|
| Date | 2011-09-28 20:49 +0100 |
| Message-ID | <timstreater-03D2CA.20495328092011@news.individual.net> |
| In reply to | #3224 |
In article <9281403.1198.1317234500379.JavaMail.geo-discussion-forums@yqma37>, tamouse <tamouse.lists@gmail.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
[toc] | [prev] | [next] | [standalone]
| From | Doug Miller <doug_at_milmacdotcom@example.com> |
|---|---|
| Date | 2011-09-29 08:09 -0400 |
| Message-ID | <j61n6n$g0k$1@dont-email.me> |
| In reply to | #3224 |
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) ?
[toc] | [prev] | [next] | [standalone]
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
|---|---|
| Date | 2011-10-01 16:08 +0200 |
| Message-ID | <3104939.FHjZrv7YcN@PointedEars.de> |
| In reply to | #3224 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web