Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Am I not doing the "or" correctly?

  • This topic is empty.
Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #165787
    amidigital
    Participant
    <?php if ( $name == Cummins || Freightliner) {
        echo 'Trucks';
        } elseif ( $name == Ford || Honda){
        echo 'cars';
        }
        ?>

    Thanks

    #165790
    Alen
    Participant
    $name = 'Cummins';
    // $name = 'Freightliner';
    // $name = 'Ford';
    // $name = 'Honda';
    
    if ( $name == 'Cummins' || $name = 'Freightliner') {
      echo 'Truck!';
    } elseif ( $name == 'Ford' || $name == 'Honda' ) {
        echo 'Cars!';
      }
    
    // Another way is to use arrays to hold data,
    // then you can just check if value is present
    
    $trucks = array('Cummins', 'Freightliner');
    $cars = array('Ford', 'Honda');
    
    // in_array( 'term', $array  )
    
    if ( in_array('Cummins', $trucks) ) {
      echo 'Trucks!';
    }
    
    if ( in_array('Ford', $cars) ) {
      echo 'Cars!';
    }
    
    #165844
    amidigital
    Participant

    Perfect thanks!

    #165892
    __
    Participant
    if ( $name == 'Cummins' || $name = 'Freightliner')
    

    correction (typo?): should be double equals (==) on both.

    #166142
    amidigital
    Participant
    if ( in_array('Cummins', $trucks) ) {
      echo 'Trucks!';
    }

    Isn’t it redundant to have Cummins and $trucks since Cummins is a truck. Why can’t you just do…

    if ( in_array($trucks) ) {
      echo 'Trucks!';
    }
    #166149
    connorblikre
    Participant

    The first parameter of “in_array” is the value you’re checking for in the array, which is the second parameter. Without the first value, the function wouldn’t know what it’s looking for.

    #166151
    __
    Participant

    Why can’t you just do…

    For future reference, this (and many more) questions can be answered by Reading The Friendly Manual.

Viewing 7 posts - 1 through 7 (of 7 total)
  • The forum ‘Back End’ is closed to new topics and replies.