strings and conditionals..

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

    strings and conditionals..

    hi, am new to PHP, trying to learn strings... I'm a bit mystifed about
    something.. how do you test for conditions in strings? for example,
    how would you do what in JavaScript (or Java) you do like this

    if (email.indexOf( '@') != -1) { // etc..

    also how do you test for length of a string? I tried:

    if (strlen($myName ) == 7) {

    but got error...

    I have looked at sections dealing with conditionals in tutorials, but
    don't see examles of this (also don't see functions like indexOf(),
    etc.. under Strings here, http://us2.php.net/manual/en/ref.strings.php..

    thank you very much..

  • Janwillem Borleffs

    #2
    Re: strings and conditionals..

    maya wrote:[color=blue]
    > hi, am new to PHP, trying to learn strings... I'm a bit mystifed
    > about something.. how do you test for conditions in strings? for
    > example, how would you do what in JavaScript (or Java) you do like
    > this
    > if (email.indexOf( '@') != -1) { // etc..
    >
    > also how do you test for length of a string? I tried:
    >[/color]

    You are looking for the position of a specific characther, so use the search
    facility on php.net by entering "position" and selecting "all php.net sites"
    or "this mirror only" from the select next to the input box. The first
    search result is the one you are looking for.
    [color=blue]
    > if (strlen($myName ) == 7) {
    >
    > but got error...
    >[/color]

    When the error is a notice, the probable cause would be that you didn't
    initialize $myName.

    If you want to learn PHP (or any other new language for that matter), the
    best way is to start from scratch; point your browser to
    http://www.php.net/manual/en/getting-started.php to start with the basics.


    JW


    Comment

    • Toby Inkster

      #3
      Re: strings and conditionals..

      maya wrote:
      [color=blue]
      > if (email.indexOf( '@') != -1) { // etc..[/color]

      if (strstr(email, '@')!==FALSE)
      {
      // etc...
      }
      [color=blue]
      > also how do you test for length of a string? I tried:
      > if (strlen($myName ) == 7) {
      > but got error...[/color]

      strlen() is correct. Your error is probably elsewhere.

      See:


      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact

      Comment

      • maya

        #4
        Re: strings and conditionals..

        Toby Inkster wrote:[color=blue]
        > maya wrote:
        >
        >[color=green]
        >>if (email.indexOf( '@') != -1) { // etc..[/color]
        >
        >
        > if (strstr(email, '@')!==FALSE)
        > {
        > // etc...
        > }
        >[/color]

        thank you.. this is exactly what I was looking for ..
        (noticed that it works both with '!==' and '!='.... there's probably
        some subtle difference I'm still not aware of....:) thank you very much..



        Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

        Comment

        • maya

          #5
          Re: strings and conditionals..

          Toby Inkster wrote:[color=blue]
          > maya wrote:
          >
          >[color=green]
          >>if (email.indexOf( '@') != -1) { // etc..[/color]
          >
          >
          > if (strstr(email, '@')!==FALSE)
          > {
          > // etc...
          > }
          >[/color]

          thank you.. this is exactly what I was looking for ..
          (noticed that it works both with '!==' and '!='.... there's probably
          some subtle difference I'm still not aware of....:) thank you very much..


          Comment

          • Rik

            #6
            Re: strings and conditionals..

            maya wrote:[color=blue]
            > thank you.. this is exactly what I was looking for ..
            > (noticed that it works both with '!==' and '!='.... there's
            > probably
            > some subtle difference I'm still not aware of....:) thank you very
            > much..[/color]

            != converted to the same type these values are not the same.
            !== these values aren't of the same value OR different types.


            !== & === compare types, for instance a boolean with the same value a string
            would hold converted to boolean won't match.



            Grtz,
            --
            Rik Wasmus


            Comment

            • ImOk

              #7
              Re: strings and conditionals..

              You can also use

              // Does string contain xyz?
              if ( ($spos=strpos(" abcxyz","xyz")) >0 ) {
              ... do code ...
              }


              maya wrote:[color=blue]
              > hi, am new to PHP, trying to learn strings... I'm a bit mystifed about
              > something.. how do you test for conditions in strings? for example,
              > how would you do what in JavaScript (or Java) you do like this
              >
              > if (email.indexOf( '@') != -1) { // etc..
              >
              > also how do you test for length of a string? I tried:
              >
              > if (strlen($myName ) == 7) {
              >
              > but got error...
              >
              > I have looked at sections dealing with conditionals in tutorials, but
              > don't see examles of this (also don't see functions like indexOf(),
              > etc.. under Strings here, http://us2.php.net/manual/en/ref.strings.php..
              >
              > thank you very much..[/color]

              Comment

              • Mladen Gogala

                #8
                Re: strings and conditionals..

                On Sun, 11 Jun 2006 15:52:55 -0400, maya wrote:
                [color=blue]
                > also how do you test for length of a string? I tried:
                >
                > if (strlen($myName ) == 7) {
                >
                > but got error...[/color]

                No errors here. May be you tried with a fake id?

                $ php -r '$myName="Mlade n"; if (strlen($myName )==6) { echo "Works\n"; }'
                Works



                --


                Comment

                • Toby Inkster

                  #9
                  Re: strings and conditionals..

                  maya wrote:
                  [color=blue]
                  > noticed that it works both with '!==' and '!='.... there's probably
                  > some subtle difference I'm still not aware of.[/color]

                  There is, and it's a fairly important difference with strstr.

                  if (1==TRUE) // true
                  if (1===TRUE) // false

                  '==' (and '!=') test whether two things are (or are not) equal-or-equivalent.

                  '===' (and '!==') test whether two things are (or are not) exactly-equal.

                  --
                  Toby A Inkster BSc (Hons) ARCS
                  Contact Me ~ http://tobyinkster.co.uk/contact

                  Comment

                  Working...