Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP if statement with multiple values for 1 variable

Posted on 2013-06-30
2
Medium Priority
?
375 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 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

721 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