Question on conditional statements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Max

    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


  • steve

    #2
    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

    Comment

    • Geoff Berrow

      #3
      Re: Question on conditional statements

      I noticed that Message-ID: <411717a4$1_3@n ews.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/

      Comment

      • Colin McKinnon

        #4
        Re: Question on conditional statements

        Geoff Berrow wrote:
        [color=blue]
        > I noticed that Message-ID: <411717a4$1_3@n ews.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.

        Comment

        • Ian.H

          #5
          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


          Comment

          • Maxim Vexler

            #6
            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';

            Comment

            • Ian.H

              #7
              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


              Comment

              • steve

                #8
                Re: 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

                Comment

                • Michael Fesser

                  #9
                  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:




                  This may lead to unexpected results in some cases, for example when
                  using assignment operators at the same time without parantheses.

                  Micha

                  Comment

                  • Michael Fesser

                    #10
                    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('#a pple#i', $line) and preg_match('#or ange#i', $line)) {
                    ...
                    }

                    The preg* functions (Perl compatible ;) are often faster and more
                    flexible than the ereg* functions.

                    Micha

                    Comment

                    • Steve

                      #11
                      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('#a pple#i', $line) and preg_match('#or ange#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?

                      Comment

                      • Michael Fesser

                        #12
                        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

                        Comment

                        • steve

                          #13
                          Re: 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

                          Comment

                          • Chung Leong

                            #14
                            Re: Question on conditional statements


                            "Steve" <ThisOne@Aint.v alid> 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('#a pple#i', $line) and preg_match('#or ange#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.


                            Comment

                            • Max

                              #15
                              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


                              Comment

                              Working...