Solved

PHP if statement with multiple values for 1 variable

Posted on 2013-06-30
2
370 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
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

825 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