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
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
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?
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...
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?
----- Original Message -----
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
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.
----- Original Message -----
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
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
----- Original Message -----
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?
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
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
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.