Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP If Statement with Multiple Conditions

I have a variable$var.

I want echo "true" if $var is equal to any of the following values abc, def, hij, klm, or nop. Is there a way to do this with a single statement like &&??

like image 231
Alfred Avatar asked Apr 08 '11 10:04

Alfred


People also ask

What are the 4 PHP conditional statements?

PHP Conditional Statements if statement - executes some code if one condition is true. if...else statement - executes some code if a condition is true and another code if that condition is false. if... elseif...else statement - executes different codes for more than two conditions.

Can IF statement have multiple conditions?

The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.

What is nested IF ELSE statement in PHP?

The nested if statement contains the if block inside another if block. The inner if statement executes only when specified condition in outer if statement is true.


9 Answers

An elegant way is building an array on the fly and using in_array():

if (in_array($var, array("abc", "def", "ghi")))

The switch statement is also an alternative:

switch ($var) {
case "abc":
case "def":
case "hij":
    echo "yes";
    break;
default:
    echo "no";
}
like image 179
Pekka Avatar answered Oct 06 '22 08:10

Pekka


if($var == "abc" || $var == "def" || ...)
{
    echo "true";
}

Using "Or" instead of "And" would help here, i think

like image 43
Tokk Avatar answered Oct 06 '22 07:10

Tokk


you can use in_array function of php

$array=array('abc', 'def', 'hij', 'klm', 'nop');

if (in_array($val,$array))
{
  echo 'Value found';
}
like image 38
Shakti Singh Avatar answered Oct 06 '22 08:10

Shakti Singh


Dont know, why you want to use &&. Theres an easier solution

echo in_array($var, array('abc', 'def', 'hij', 'klm', 'nop'))
      ? 'yes' 
      : 'no';
like image 28
KingCrunch Avatar answered Oct 06 '22 06:10

KingCrunch


you can use the boolean operator or: ||

if($var == 'abc' || $var == 'def' || $var == 'hij' || $var == 'klm' || $var == 'nop'){
    echo "true";
}
like image 41
sauerburger Avatar answered Oct 06 '22 06:10

sauerburger


I found this method worked for me:

$thisproduct = "my_product_id";
$array=array("$product1", "$product2", "$product3", "$product4");
if (in_array($thisproduct,$array)) {
    echo "Product found";
}
like image 24
IC360 Oliver Cannell Avatar answered Oct 06 '22 07:10

IC360 Oliver Cannell


You can try this:

<?php
    echo (($var=='abc' || $var=='def' || $var=='hij' || $var=='klm' || $var=='nop') ? "true" : "false");
?>
like image 45
Osh Mansor Avatar answered Oct 06 '22 08:10

Osh Mansor


Sorry to resurrect this, but I stumbled across it & believe it adds value to the question.

In PHP 8.0.0^ you can now use the match expression like so:

<?php
echo match ($var) {
    'abc','def','hij','klm' => 'true',
};
?>
//echos 'true' as a string

Working link from OnlinePHPfunctions

PHP Manual

like image 28
Shaun Moore Avatar answered Oct 06 '22 06:10

Shaun Moore


Try this piece of code:

$first = $string[0]; 
if($first == 'A' || $first == 'E' || $first == 'I' || $first == 'O' || $first == 'U') {
   $v='starts with vowel';
} 
else {
   $v='does not start with vowel';
}
like image 35
paul Avatar answered Oct 06 '22 06:10

paul