Solved

PHP if statement with multiple values for 1 variable

Posted on 2013-06-30
2
373 Views
Last Modified: 2013-06-30
Let's say I want to write the following conditional:

if ($letter == "a") || ($letter == "b") || ($letter == "c") {
    do something;
}

Open in new window

Is there a simpler way to write it?  For example:

if ( ($letter == ( "a" || "b" || "c" ) ) {
     do something;
}

Open in new window

I don't think my alternate version will work, but am I wrong, or is there something similar that will work?

Thanks!
0
Comment
Question by:Jonathan Greenberg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
Mysidia earned 500 total points
ID: 39288518
No, that will not work.   The ||   (OR) operator   ORs the logical value of an expression

Here's how that IF statement would be evaluated:
              if ( ($letter == ( "a" || "b" || "c" ) )

             Due to the parenthesis,  "a"||"b"||"c"  will be evaluated first.
             Within that expression   "a"||"b"  will be evaluated first.
             Both are non-null literals, so  it will be true  (result is  1).

             Next  PHP's interpreter will evaluate    1 || "c"
             Since the left hand side is true,   1 || "c"  evaluates to true,
             And the  whole parenthesized expression evaluates to 1,
             so the if statement is equivalent to
             
              if ( ($letter == (1) )

              This will   generally evaluate to null,  so the body of the IF statement won't be executed.


You could do  this:

$myhash = array('a' => '1', 'b' => '1', 'c' => '1');

if ($myhash[$letter] == 1) {
    do something;
}


The idea is you  index an array  or  "table"  based on the  letter you are looking for.

If the letter you see is a member of the array,  then you execute the body of the IF statement.
0
 

Author Comment

by:Jonathan Greenberg
ID: 39288540
Thank you, Mysidia.  Great explanation!

Regards,
Jon
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question