Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP if statement with multiple values for 1 variable

Posted on 2013-06-30
2
Medium Priority
?
376 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
2 Comments
 
LVL 23

Accepted Solution

by:
Mysidia earned 2000 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

Independent Software Vendors: 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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

877 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