In PHP, Is There a Short Way to Compare a Variable to Multiple Values

In PHP, is there a short way to compare a variable to multiple values?

in_array() is what I use

if (in_array($variable, array('one','two','three'))) {

Compare multiple values in PHP

Place the values in an array, then use the function in_array() to check if they exist.

$checkVars = array(3, 4, 5, "string", "2010-05-16");
if(in_array($var, $checkVars)){
// Value is found.
}

http://uk.php.net/manual/en/function.in-array.php

What is a shorthand way for checking that multiple variables are ALL equal to the same value in an IF statement? (PHP)

array_flip is several times faster than array_unique:

function all_equal($arr, $value) {
return array_keys(array_flip($arr)) == array($value);
}

$arr = array($tstat, $l1stat, $l2stat, $l3stat);

echo all_equal($arr, 'no_prices');

A quick profile for the answers given thus far, for 1000 iterations on array length 1000:

  array_flip: 0.07321620 seconds
array_unique: 0.32569408 seconds
foreach: 0.15136194 seconds
array_filter: 0.41404295 seconds

The code used to profile is here: http://codepad.org/szgNfWHe

Note: As @cypherabe rightly points out, array_flip does not overtake array_unique until the array has at least 5 elements, and does not overtake foreach until the array has at least 10 elements.

Simpler way to check if variable is not equal to multiple string values?

For your first code, you can use a short alteration of the answer given by
@ShankarDamodaran using in_array():

if ( !in_array($some_variable, array('uk','in'), true ) ) {

or even shorter with [] notation available since php 5.4 as pointed out by @Forty in the comments

if ( !in_array($some_variable, ['uk','in'], true ) ) {

is the same as:

if ( $some_variable !== 'uk' && $some_variable !== 'in' ) {

... but shorter. Especially if you compare more than just 'uk' and 'in'.
I do not use an additional variable (Shankar used $os) but instead define the array in the if statement. Some might find that dirty, i find it quick and neat :D

The problem with your second code is that it can easily be exchanged with just TRUE since:

if (true) {

equals

if ( $some_variable !== 'uk' || $some_variable !== 'in' ) {

You are asking if the value of a string is not A or Not B. If it is A, it is definitely not also B and if it is B it is definitely not A. And if it is C or literally anything else, it is also not A and not B. So that statement always (not taking into account schrödingers law here) returns true.

PHP how to compare strings between two variables and display only unique strings?

As Siphalor stated here is an implementation

$a = 'this car is very beautiful and is the fast';
$b = 'this red car is very beautiful and is the fast that others';
echo $unique_words = show_unique_strings($a, $b);
//expected output(painted on the screen): red that others

function show_unique_strings($a, $b) {
$aArray = explode(" ",$a);
$bArray = explode(" ",$b);
$intersect = array_intersect($aArray, $bArray);
return implode(" ", array_merge(array_diff($aArray, $intersect), array_diff($bArray, $intersect)));
}

How to get multiple values from a single select variable in HTML/PHP?

you can pass the two values in the value

<select name="size" id="size" type="text">
....
<option value="6x4" >6 Inches by 4 Inches</option>
</select>

and in the backend you can split it to get the value

list($x,$y) = explode("x",$_GET['size']);  // or POST

echo $x; // 6
echo $y; // 4

Is there a way to have multiple values in one if statement?

Something like this:

if (in_array('needle', array('value1', 'value2', 'needle'))){

}


Related Topics



Leave a reply



Submit