Solved

PHP if statement with multiple values for 1 variable

Posted on 2013-06-30
2
362 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 500 total points
Comment Utility
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
Comment Utility
Thank you, Mysidia.  Great explanation!

Regards,
Jon
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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 …

744 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now