Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    b99
    b99 is offline
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cant search Google for PHP OR examples! Argh, please can you help?

    Hi Guys,

    Great forum by the way!

    I am having a little problem and wondered if any of you nice people would be kind enough to help me out?

    I am having problems with a php if else statement.

    Basically I am trying to tell php to echo something if the 1st letter equals 'a' or 'b'.

    for example, this works fine and echos 'test' if the word begins with 'a':

    PHP Code:
    if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a'))
         {
        echo 
    'test';
            } else {
        echo 
    '';
        } 
    however the following echos 'test' no matter what the word begins with:

    PHP Code:
    if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || 'b'))
         {
        echo 
    'test';
            } else {
        echo 
    '';
        } 
    Am i missing something?

    I cant search google because it ignores the word 'or'!!!!!


    Any help would be much appreciated,

    Many thanks

    Mike

    ps: i have also tried the following with no luck;


    PHP Code:
    if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || (substr(strip_tags($_GET['ps']),0,1) == 'b'))
         {
        echo 
    'test';
            } else {
        echo 
    '';
        } 
    Last edited by b99; 06-20-2010 at 04:48 PM. Reason: typo

  • #2
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Try this
    PHP Code:
    <?php
    if ($example == NULL && substr(strip_tags($_GET['ps']),0,1) == 'a' ||
        
    $example == NULL && substr(strip_tags($_GET['ps']),0,1) == 'b')
    {
      echo 
    'test';
    }
    else
    {
      echo 
    '';
    }
    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    You made a typo in your last example, with too many parenthesis :-P

    PHP Code:
    if ($example == NULL & (substr(strip_tags($_GET['ps']),0,1) == 'a' || substr(strip_tags($_GET['ps']),0,1) == 'b'))
         {
        echo 
    'test';
            } else {
        echo 
    '';
        } 
    You need to drop the open parenthesis before the second substr and it works fine.

    In PHP, at least in any way I have found so far, you can't chain OR statements when doing a comparison. How I get around it is using an array and in_array:

    PHP Code:
    $needle substr(strip_tags($_GET['ps']),0,1);

    if (
    $example == NULL in_array($needle, array('a''b'))
         {
        echo 
    'test';
            } else {
        echo 
    '';
        } 

  • #4
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by Keleth View Post
    In PHP, at least in any way I have found so far, you can't chain OR statements when doing a comparison.
    You can as the code I wrote checks both 'a' and 'b' so if it doesn't equal 'a' it then checks to see if it equals 'b' if it doesn't equal 'a' or 'b' then it wont pass but if it equals one of them the it will pass.
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P

  • #5
    b99
    b99 is offline
    New to the CF scene
    Join Date
    Jun 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks guys!

    Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!

    Thumbs up!

  • #6
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,546
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by DJCMBear View Post
    You can as the code I wrote checks both 'a' and 'b' so if it doesn't equal 'a' it then checks to see if it equals 'b' if it doesn't equal 'a' or 'b' then it wont pass but if it equals one of them the it will pass.
    Err... I meant in the way he first used it, trying to do something = a || b.

    And anyway, his last example was fine as well, checked if it was a or b, but with less code :-P He just had an extra parenthesis.

    I prefer the array method just for coder readability. When I'm checking for the same thing in many different places, I create a page with some constant variables and include it on w/e pages need it, use an in_array test to check for each one.

    Quote Originally Posted by b99 View Post
    Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!
    Do you plan on just testing the start of the variable for certain characters? Are you going to search for strings? The method above is ok for a few characters, two or three, but after that I'd go with a regex based on what you're doin.
    Last edited by Keleth; 06-20-2010 at 05:20 PM.

  • #7
    Senior Coder DJCMBear's Avatar
    Join Date
    Mar 2010
    Location
    United Kindom
    Posts
    1,173
    Thanks
    14
    Thanked 136 Times in 136 Posts
    Quote Originally Posted by b99 View Post
    thanks guys!

    Very very very much appreciated, I thought there must be way to make it work and keleth you array and in_array will shorten the code massively, especially when im going to be doing all the letters of the alphabet!

    Thumbs up!
    To make your life so much easier why dont you use a php function to do all the work.

    Like this:
    PHP Code:
    <?php
    function checkFirst($str)
    {
      
    $str substr(strip_tags($str),0,1);
      
    $letters = array('a','b','c','e','f','g','h','i','j','k','l','m','n',
                       
    'o','p','q','r','s','t','u','v','w','x','y','z');
      return 
    in_array($str,$letters);
    }

    if (
    $example == NULL checkFirst($_GET['ps'])) 

      echo 
    'test'
    }
    else

      echo 
    ''


    ?>
    Official BinPress hand picked coder.
    For anyone worried about SQL injection go have a look at my small yet powerful script here.
    Go Pledge for Light Table, if it hit's $300,000 Python and other languages will get added.
    I am 1 of 65,608 people to get a Pebble Watch :P


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •