While-else

php.internals

Pelle Ravn Rosfeldt

19 years ago
Hi there. Is it possible to make a "while" with a "else"-statement in PHP6? I know that it's not the first time this subject is up, but my research shows that a lot of people miss it. Including me. Here's an example of what I mean: --------------------- <? $i = 1; while ($i <= 10) { echo $i++; } else { echo "The while couldn't be executed!"; } ?> --------------------- Python & Perl already have this function. So why not PHP? It could be very useful in an SQL query, if it doesn't return anything. Best Regards Pelle Ravn

Asbjørn Sloth Tønnesen

19 years ago
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Pelle Ravn Rosfeldt wrote:
> Hi there. > > Is it possible to make a "while" with a "else"-statement in PHP6? > I know that it's not the first time this subject is up, but my research > shows that a lot of people miss it. Including me.
There's already a bug for it: http://bugs.php.net/bug.php?id=26411
> Here's an example of what I mean: > --------------------- > <? > $i = 1; > while ($i <= 10) { > echo $i++; > } else { > echo "The while couldn't be executed!"; > } > ?> > ---------------------
Your example never enters the else part of the statement. A better example would be using $i = 11 or higher value. - -- Best regards Asbjørn Sloth Tønnesen -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFGM44gSViWlxucwuoRAlIrAJ9Xi5y7MkJCkNfW9lqtQ255D5uOWACeKNmU bNnKnuzxJhozYwchyZlOVzw= =CD+b -----END PGP SIGNATURE-----

Pelle Ravn Rosfeldt

19 years ago
-----Original Message----- From: Asbjørn Sloth Tønnesen [mailto:asbjorn@asbjorn.biz] Sent: 28. april 2007 20:11 To: Pelle Ravn Rosfeldt Cc: internals@lists.php.net Subject: Re: [PHP-DEV] While-else -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Pelle Ravn Rosfeldt wrote:
> Hi there. > > Is it possible to make a "while" with a "else"-statement in PHP6? > I know that it's not the first time this subject is up, but my research > shows that a lot of people miss it. Including me.
> There's already a bug for it: http://bugs.php.net/bug.php?id=26411
I see.
> Here's an example of what I mean: > --------------------- > <? > $i = 1; > while ($i <= 10) { > echo $i++; > } else { > echo "The while couldn't be executed!"; > } > ?> > ---------------------
> Your example never enters the else part of the statement. A better > example would be using $i = 11 or higher value.
Yeah. You are absolutely right! My example is just to show how is should look. - -- Best regards Asbjørn Sloth Tønnesen -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.6 (GNU/Linux) iD8DBQFGM44gSViWlxucwuoRAlIrAJ9Xi5y7MkJCkNfW9lqtQ255D5uOWACeKNmU bNnKnuzxJhozYwchyZlOVzw= =CD+b -----END PGP SIGNATURE-----

Oliver Block

19 years ago
Am Samstag, 28. April 2007 20:03 schrieb Pelle Ravn Rosfeldt:
> Python & Perl already have this function. > It could be very useful in an SQL query, if it doesn't return anything.
Perl: "There's always more than one way to do it." Best Regards, Oliver

Richard Lynch

19 years ago
On Sat, April 28, 2007 1:03 pm, Pelle Ravn Rosfeldt wrote:
> Is it possible to make a "while" with a "else"-statement in PHP6? > I know that it's not the first time this subject is up, but my > research > shows that a lot of people miss it. Including me. > > Here's an example of what I mean: > --------------------- > <? > $i = 1; > while ($i <= 10) { > echo $i++; > } else { > echo "The while couldn't be executed!"; > } > ?> > ---------------------
So it executes after the last iteration? No. It must only execute if there were no iterations?... Maybe I'm just an old fart, but I don't like it... :-v
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?

Ron Korving

19 years ago
It can save you one if-statement, where the condition would be identical to the where condition. Example: if ($i <= 10) { while ($i <= 10) { $i++; } } else { echo "$i is more than 10\n"; } I personally hate overspecifications, and this is one situation where that's the case. So actually, I would like while-else support. Just my humble 2 futile cents of course. Regards, Ron ""Richard Lynch"" <ceo@l-i-e.com> schreef in bericht news:35014.216.230.84.67.1178133032.squirrel@www.l-i-e.com...

Andrew Brampton

19 years ago
----- Original Message -----
> On Sat, April 28, 2007 1:03 pm, Pelle Ravn Rosfeldt wrote: >> Is it possible to make a "while" with a "else"-statement in PHP6? >> I know that it's not the first time this subject is up, but my >> research >> shows that a lot of people miss it. Including me. >> >> Here's an example of what I mean: >> --------------------- >> <? >> $i = 1; >> while ($i <= 10) { >> echo $i++; >> } else { >> echo "The while couldn't be executed!"; >> } >> ?> >> ---------------------
I have wanted to use this kind of syntax a couple of times, for example: <?php while ($line = mysql_fetch_array(...)) { echo $line; } else { echo 'Sorry no records'; } ?> Otherwise I would write the above code as a while + a if. However I'm unsure if this syntax should be put into PHP, but I thought I'll just post a example of when I would use it. thanks Andrew

Sean Bright

19 years ago
This fails the "Make PHP code easier to read" test I think. But it passes the "I'm a lazy programmer" test with flying colors! ;-) I mean seriously, what real value does this add to the language? Its just syntactic sugar to save yourself a few keystrokes. On 5/3/07, Andrew Brampton <andrew@bramp.freeserve.co.uk> wrote:

Alain Williams

19 years ago
On Thu, May 03, 2007 at 08:36:00AM -0400, Sean Bright wrote:
> This fails the "Make PHP code easier to read" test I think. But it passes > the "I'm a lazy programmer" test with flying colors! ;-) > > I mean seriously, what real value does this add to the language? Its just > syntactic sugar to save yourself a few keystrokes.
* Less code - smaller compiled code * Only do the test once, without the need of extra variables/... I wonder if this should also be done on foreach: foreach($someArray as $key => $value) { echo "key=$key value=$value\n"; } else { echo "someArray is empty"; } $someArray could have been a function call. Also should this happen with the alternative syntax: while($i <= 10) : echo $i++; else: echo "The while couldn't be executed!"; endwhile;
-- Alain Williams Linux Consultant - Mail systems, Web sites, Networking, Programmer, IT Lecturer. +44 (0) 787 668 0256 http://www.phcomp.co.uk/ Parliament Hill Computers Ltd. Registration Information: http://www.phcomp.co.uk/contact.php #include <std_disclaimer.h>

Richard Lynch

19 years ago
On Thu, May 3, 2007 2:34 am, Andrew Brampton wrote:
> ----- Original Message ----- >> On Sat, April 28, 2007 1:03 pm, Pelle Ravn Rosfeldt wrote: >>> Is it possible to make a "while" with a "else"-statement in PHP6? >>> I know that it's not the first time this subject is up, but my >>> research >>> shows that a lot of people miss it. Including me. >>> >>> Here's an example of what I mean: >>> --------------------- >>> <? >>> $i = 1; >>> while ($i <= 10) { >>> echo $i++; >>> } else { >>> echo "The while couldn't be executed!"; >>> } >>> ?> >>> --------------------- > > I have wanted to use this kind of syntax a couple of times, for > example: > <?php > while ($line = mysql_fetch_array(...)) { > echo $line; > } else { > echo 'Sorry no records'; > } > ?> > > Otherwise I would write the above code as a while + a if. > > However I'm unsure if this syntax should be put into PHP, but I > thought I'll > just post a example of when I would use it.
I can understand how it works now, and where you'd use it... And I still don't care for it... I mean, 'else' just doesn't go with 'while', to me... And you rarely need something as convoluted as the other example. while ($line = mysql_fetch_array(...)){ echo $line; //not really right, btw... :-) } if (!mysql_num_rows(...)){ echo "no records"; } So, really, it just makes confusing PHP code, that doesn't seem to save much, in most real-world cases I can think of.
-- Some people have a "gift" link here. Know what I want? I want you to buy a CD from some indie artist. http://cdbaby.com/browse/from/lynch Yeah, I get a buck. So?

Oliver Block

19 years ago
Am Donnerstag, 3. Mai 2007 09:34 schrieb Andrew Brampton:
> I have wanted to use this kind of syntax a couple of times, for example: > <?php > while ($line = mysql_fetch_array(...)) { > echo $line; > } else { > echo 'Sorry no records'; > } > ?>
This example leaves me speechless.:) Regard, Oliver

Robin Vickery

19 years ago
On 28/04/07, Pelle Ravn Rosfeldt <PelleRR@it-college.dk> wrote:
> Hi there. > > Is it possible to make a "while" with a "else"-statement in PHP6? > I know that it's not the first time this subject is up, but my research > shows that a lot of people miss it. Including me. > > Here's an example of what I mean: > --------------------- > <? > $i = 1; > while ($i <= 10) { > echo $i++; > } else { > echo "The while couldn't be executed!"; > } > ?> > --------------------- > > Python & Perl already have this function. So why not PHP?
The 'but everyone else is doing it' argument is particularly weak when it's not actually accurate. $ man perlsyn
> The following compound statements may be used to control flow: > [...] > LABEL while (EXPR) BLOCK > LABEL while (EXPR) BLOCK continue BLOCK
$ perl -le '$i = 99; while ($i < 10) { $i++; } else { print "Oh..."; }' syntax error at -e line 1, near "} else" I can't see any point in it myself. It doesn't read naturally and it doesn't add anything that you don't get by preceding your while with a simple if statement. -robin

Paul Colomiets

19 years ago
Pelle Ravn Rosfeldt wrote:
> Python & Perl already have this function. So why not PHP? > It could be very useful in an SQL query, if it doesn't return anything. >
in Python else clause is executed if while is not ended by break. So it makes sense when you searching for something: <? $i = 1; while ($i <= 10) { echo $i++; if($i == $j) { echo "Found"; break; } } else { echo "Not found!"; } ?> It can't be easily written with if. But your case can be.
-- Regards, Paul.