Question on conditional statements | | |
Hi,
I'm a perl programmer and am trying to learn PHP.
So far I have figured out most of the differences, but have not been able to
find out how to do the following:
When running through a loop, how can you test two separate conditions
against the same $element of an array. For example, this is how I thought it
would be done (similar to Perl), but it did not work:
if (eregi("apple", $line) and (eregi("orange", $line) {
do whatever;
}
Basically testing each line of an array to see if both the words "apple" and
"orange" are present.
Also, is there any equivalent to the following from Perl while running
through a loop:
next if (whatever conditions);
last if (whatever conditions);
Thanks,
Max | | | | re: Question on conditional statements
"Max46" wrote:[color=blue]
> Hi,
>
> I’m a perl programmer and am trying to learn PHP.
>
> So far I have figured out most of the differences, but have not[/color]
been[color=blue]
> able to
> find out how to do the following:
>
> When running through a loop, how can you test two separate[/color]
conditions[color=blue]
> against the same $element of an array. For example, this is how I
> thought it
> would be done (similar to Perl), but it did not work:
>
> if (eregi("apple", $line) and (eregi("orange", $line) {
>
> do whatever;
>
> }
>
> Basically testing each line of an array to see if both the words
> "apple" and
> "orange" are present.
>
> Also, is there any equivalent to the following from Perl while[/color]
running[color=blue]
> through a loop:
>
> next if (whatever conditions);
> last if (whatever conditions);
>
> Thanks,
> Max[/color]
Intead of "and" use "&&", instead of "or" use "||".
I dont know the answer to your other question.
-- http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-conditio...ict137697.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=460176 | | | | re: Question on conditional statements
I noticed that Message-ID: <411717a4$1_3@news.athenanews.com> from steve
contained the following:
[color=blue][color=green]
> > if (eregi("apple", $line) and (eregi("orange", $line) {
> >
> > do whatever;
> >
> > }
> >
> > Basically testing each line of an array to see if both the words
> > "apple" and
> > "orange" are present.
> >
> > Also, is there any equivalent to the following from Perl while[/color]
>running[color=green]
> > through a loop:
> >
> > next if (whatever conditions);
> > last if (whatever conditions);
> >
> > Thanks,
> > Max[/color]
>
>Intead of "and" use "&&", instead of "or" use "||".[/color]
According to the manual 'and' should work. There is a bracket missing
in the code above though.[color=blue]
>
>I dont know the answer to your other question.[/color]
elseif or use switch
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/ | | | | re: Question on conditional statements
Geoff Berrow wrote:
[color=blue]
> I noticed that Message-ID: <411717a4$1_3@news.athenanews.com> from steve
> contained the following:[color=green][color=darkred]
>> >
>> > Also, is there any equivalent to the following from Perl while[/color]
>>running[color=darkred]
>> > through a loop:
>> >
>> > next if (whatever conditions);
>> > last if (whatever conditions);
>> >
>> > Thanks,
>> > Max[/color]
>>
>>
>>I dont know the answer to your other question.[/color]
>
> elseif or use switch
>[/color]
My Perl is a bit rusty but I think he really wants to use continue / break;
HTH
C. | | | | re: Question on conditional statements
On Mon, 09 Aug 2004 02:20:20 -0400, steve wrote:
[color=blue]
> "Max46" wrote:[color=green]
> > Hi,
> >
> > I’m a perl programmer and am trying to learn PHP.
> >
> > So far I have figured out most of the differences, but have not[/color]
> been[color=green]
> > able to
> > find out how to do the following:
> >
> > When running through a loop, how can you test two separate[/color]
> conditions[color=green]
> > against the same $element of an array. For example, this is how I
> > thought it
> > would be done (similar to Perl), but it did not work:
> >
> > if (eregi("apple", $line) and (eregi("orange", $line) {
> >
> > do whatever;
> >
> > }
> >
> > Basically testing each line of an array to see if both the words
> > "apple" and
> > "orange" are present.
> >
> > Also, is there any equivalent to the following from Perl while[/color]
> running[color=green]
> > through a loop:
> >
> > next if (whatever conditions);
> > last if (whatever conditions);
> >
> > Thanks,
> > Max[/color]
>
> Intead of "and" use "&&", instead of "or" use "||".[/color]
Umm, why? what's wrong with 'and' and 'or'? I use them all the time.
if (eregi('apple', $line) or eregi('orange', $line)) {
/* do stuff... */
}
But preg_*() function should be used rather than eregi calls.
Regards,
Ian
PS: The issue with the OP's code is 2 missing )
--
Ian.H
digiServ Network
London, UK http://digiserv.net/ | | | | re: Question on conditional statements
Ian.H wrote:[color=blue]
> On Mon, 09 Aug 2004 02:20:20 -0400, steve wrote:
>
>[color=green]
>>"Max46" wrote:[color=darkred]
>> > Hi,
>> >
>> > I’m a perl programmer and am trying to learn PHP.
>> >
>> > So far I have figured out most of the differences, but have not[/color]
>>been[color=darkred]
>> > able to
>> > find out how to do the following:
>> >
>> > When running through a loop, how can you test two separate[/color]
>>conditions[color=darkred]
>> > against the same $element of an array. For example, this is how I
>> > thought it
>> > would be done (similar to Perl), but it did not work:
>> >
>> > if (eregi("apple", $line) and (eregi("orange", $line) {
>> >
>> > do whatever;
>> >
>> > }
>> >
>> > Basically testing each line of an array to see if both the words
>> > "apple" and
>> > "orange" are present.
>> >
>> > Also, is there any equivalent to the following from Perl while[/color]
>>running[color=darkred]
>> > through a loop:
>> >
>> > next if (whatever conditions);
>> > last if (whatever conditions);
>> >
>> > Thanks,
>> > Max[/color]
>>
>>Intead of "and" use "&&", instead of "or" use "||".[/color]
>
>
>
> Umm, why? what's wrong with 'and' and 'or'? I use them all the time.
>
>
> if (eregi('apple', $line) or eregi('orange', $line)) {
> /* do stuff... */
> }
>
>
> But preg_*() function should be used rather than eregi calls.
>
>
>
> Regards,
>
> Ian
>
>
> PS: The issue with the OP's code is 2 missing )
>[/color]
i prefer && and || instead of AND and OR because it's more C'sh
(and, or) -> just smells M$ to me, sorry :)
if ($A == $B) { print '$A == $B'; }
elseif ( ($A >= $C) || !($A % 2) ) { print '($A >= $C) || ($A % 2)'; }
else print 'else'; | | | | re: Question on conditional statements
On Mon, 09 Aug 2004 14:50:11 +0000, Maxim Vexler <hq4ever (at) 012 (dot)
net (dot) il> wrote:
[ snip ]
[color=blue]
> i prefer && and || instead of AND and OR because it's more C'sh
>
> (and, or) -> just smells M$ to me, sorry :)
>
> if ($A == $B) { print '$A == $B'; }
> elseif ( ($A >= $C) || !($A % 2) ) { print '($A >= $C) || ($A % 2)'; }
> else print 'else';[/color]
Heh, I can see your point on that looking VBish, but they actually work
differently rather than just being personal choice as such. Dependng on
the conditional I'm using will depend on whether I use 'and' or && =)
I guess my "all the time" statement was a little inaccurate.
Regards,
Ian
--
Ian.H
digiServ Network
London, UK http://digiserv.net/ | | | | re: Question on conditional statements
"Ian.H" wrote:[color=blue]
> On Mon, 09 Aug 2004 14:50:11 +0000, Maxim Vexler <hq4ever (at) 012
> (dot)
> net (dot) il> wrote:
>
>
> [ snip ]
>
>[color=green]
> > i prefer && and || instead of AND and OR because[/color]
> it’s more C’sh[color=green]
> >
> > (and, or) -> just smells M$ to me, sorry
> >
> > if ($A == $B) { print ’$A == $B’; }
> > elseif ( ($A >= $C) || !($A % 2) ) { print ’($A >=[/color]
> $C) || ($A % 2)’; }[color=green]
> > else print ’else’;[/color]
>
>
> Heh, I can see your point on that looking VBish, but they actually
> work
> differently rather than just being personal choice as such.[/color]
Dependng[color=blue]
> on
> the conditional I’m using will depend on whether I use
> ’and’ or && =)
>
> I guess my "all the time" statement was a little inaccurate.
>
>
>
> Regards,
>
> Ian
>[/color]
I had problems with AND, OR, so I started using the other notation. I
don’t quite know how exactly they work, but I know they did not work
as advertised for me (but then I was a newbie). Has anyone seen this
problem?
-- http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-conditio...ict137697.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=460658 | | | | re: Question on conditional statements
.oO(steve)
[color=blue]
>I had problems with AND, OR, so I started using the other notation. I
>don’t quite know how exactly they work, but I know they did not work
>as advertised for me (but then I was a newbie). Has anyone seen this
>problem?[/color]
The only difference between and/&& and or/|| is their precedence: http://www.php.net/manual/en/language.operators.php http://www.php.net/manual/en/languag...rs.logical.php
This may lead to unexpected results in some cases, for example when
using assignment operators at the same time without parantheses.
Micha | | | | re: Question on conditional statements
.oO(Max)
[color=blue]
>When running through a loop, how can you test two separate conditions
>against the same $element of an array. For example, this is how I thought it
>would be done (similar to Perl), but it did not work:
>
>if (eregi("apple", $line) and (eregi("orange", $line) {
>
> do whatever;
>
>}[/color]
There are some parse errors because of misplaced/missing parantheses.
Try this:
if (eregi("apple", $line) and eregi("orange", $line)) {
...
}
Or better:
if (preg_match('#apple#i', $line) and preg_match('#orange#i', $line)) {
...
}
The preg* functions (Perl compatible ;) are often faster and more
flexible than the ereg* functions.
Micha | | | | re: Question on conditional statements
Michael Fesser wrote:[color=blue]
> .oO(Max)
>
>[color=green]
>>When running through a loop, how can you test two separate conditions
>>against the same $element of an array. For example, this is how I thought it
>>would be done (similar to Perl), but it did not work:
>>
>>if (eregi("apple", $line) and (eregi("orange", $line) {
>>
>> do whatever;
>>
>>}[/color]
>
>
> There are some parse errors because of misplaced/missing parantheses.
> Try this:
>
> if (eregi("apple", $line) and eregi("orange", $line)) {
> ...
> }
>
>
> Or better:
>
> if (preg_match('#apple#i', $line) and preg_match('#orange#i', $line)) {
> ...
> }
>
> The preg* functions (Perl compatible ;) are often faster and more
> flexible than the ereg* functions.
>
> Micha[/color]
....wouldn't strpos() be even faster? | | | | re: Question on conditional statements
.oO(Steve)
[color=blue]
>...wouldn't strpos() be even faster?[/color]
Hmm, quite possible in this case, because the script only has to look
for complete words, no complex pattern.
I've done a little test on my machine with case-insensitive searching,
each function called 10.000 times. The results from fastest to slowest:
stristr() - 1,8s
stripos() - 2,0s (PHP5)
preg_match() - 2,8s
eregi() - 4,8s
Micha | | | | re: Question on conditional statements
"Michael Fesser" wrote:[color=blue]
> .oO(Steve)
>[color=green]
> >...wouldn’t strpos() be even faster?[/color]
>
> Hmm, quite possible in this case, because the script only has to[/color]
look[color=blue]
> for complete words, no complex pattern.
>
> I’ve done a little test on my machine with case-insensitive
> searching,
> each function called 10.000 times. The results from fastest to
> slowest:
>
> stristr() - 1,8s
> stripos() - 2,0s (PHP5)
> preg_match() - 2,8s
> eregi() - 4,8s
>
> Micha[/color]
I have found case-insensitive preg_match to be much slower than case
sensitive preg_match. I don’t have any data, but that is my
observation.
-- http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-conditio...ict137697.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=461375 | | | | re: Question on conditional statements
"Steve" <ThisOne@Aint.valid> wrote in message
news:cf8fq6$79b$1@lust.ihug.co.nz...[color=blue]
> Michael Fesser wrote:[color=green]
> > .oO(Max)
> >
> >[color=darkred]
> >>When running through a loop, how can you test two separate conditions
> >>against the same $element of an array. For example, this is how I[/color][/color][/color]
thought it[color=blue][color=green][color=darkred]
> >>would be done (similar to Perl), but it did not work:
> >>
> >>if (eregi("apple", $line) and (eregi("orange", $line) {
> >>
> >> do whatever;
> >>
> >>}[/color]
> >
> >
> > There are some parse errors because of misplaced/missing parantheses.
> > Try this:
> >
> > if (eregi("apple", $line) and eregi("orange", $line)) {
> > ...
> > }
> >
> >
> > Or better:
> >
> > if (preg_match('#apple#i', $line) and preg_match('#orange#i', $line)) {
> > ...
> > }
> >
> > The preg* functions (Perl compatible ;) are often faster and more
> > flexible than the ereg* functions.
> >
> > Micha[/color]
> ...wouldn't strpos() be even faster?[/color]
Not necessarily. While preg_match() incurs the overhead of compiling the
regexp, the regular expression engine is smarter than plain old strpos().
When the situation permits it will use a more optimal search technique,
which pays off when the subject is long. | | | | re: Question on conditional statements
> > next if (whatever conditions);[color=blue][color=green]
> > last if (whatever conditions);
> >[/color][/color]
[color=blue]
> Intead of "and" use "&&", instead of "or" use "||".
>
> I dont know the answer to your other question.[/color]
Thanks to everyone for the and/or help. I've got it working now, I think I
had a missing parenthesis.
Concerning the next if/last if, this is a way skipping to the next element
in the array with a quick one line statement, or stopping the foreach loop
if a certain condition exists. I guess there is no similar method in PHP,
you just have to go through an entire if-then structure.
Thanks,
Max | | | | re: Question on conditional statements
On Tue, 10 Aug 2004 13:34:27 +0000, Max wrote:
[ snip ]
[color=blue]
> Concerning the next if/last if, this is a way skipping to the next element
> in the array with a quick one line statement, or stopping the foreach loop
> if a certain condition exists. I guess there is no similar method in PHP,
> you just have to go through an entire if-then structure.[/color]
for ($i = 0; $i < 1000; $i++) {
if ($i == 20) break;
}
$i will never be 21 =)
PHP is the opposite way around in some things from Perl.. the if() has to
come before the break in PHP, but works the same.
Regards,
Ian
--
Ian.H
digiServ Network
London, UK http://digiserv.net/ |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,419 network members.
|