Compare Multiple Values in PHP

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

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'))) {

PHP check value against multiple values with OR-operator

The logical ||(OR) operator doesn't work as you expect it to work. The || operator always evaluates to a boolean either TRUE or FALSE. So in your example your strings get converted into booleans and then compared.

If statement:

if($ext == ('txt' || 'rtf'|| 'log' || 'docx'))

Comes down to:

if($ext == (TRUE || TRUE || TRUE || TRUE))
if($ext == TRUE)

To solve this problem and get the code to work as you want it to you can use different methods.

Multiple comparison

One way to solve the problem and check your values against multiple values is, to actually compare the value against multiple values:

if($ext == "txt" || $ext == "rtf" /* || ... */)

in_array()

Another way is to use the function in_array() and check if the value is equal to one of the array values:

if(in_array($ext, ["txt", "rtf" /* , ... */], TRUE))

Note: Second parameter is for strict comparison

switch()

You could also use switch to check your value against multiple values and just let the case fall through.

switch($ext){

case "txt":
case "rtf":
/* case ...: */
$pClass = "text-";
break;

}

how to compare a variable with multiple values

Firstly, you have a fairly straight-forward syntax error: the syntax of an if statement is if ( condition ) Note those parentheses - they're part of the if statement itself, not the condition. When you write this:

if ( $country <> "Belgique" )

The condition is this:

$country <> "Belgique"

So if you want the condition to be this:

($country <> "Belgique") or ($country <> "Luxembourg") or ($country <> "France") or ($country <> "Pays-bas")

Then you have to put the whole thing inside the parentheses of the if statement:

if ( ($country <> "Belgique") or ($country <> "Luxembourg") or ($country <> "France") or ($country <> "Pays-bas") )

Secondly, you have made a common mistake in translating English (or whatever your first language is) into formal logic: it's tempting to assume that ($country <> "Belgique") or ($country <> "Luxembourg") means "$country is neither Belgique nor Luxembourg", but it actually means "if $country is not Belgique then evaluate to true; also, if $country is not Luxembourg then evaluate to true".

If you provide "Luxembourg", the first part is true; if you provide "Belgique", the second part is true; in fact, since no value of $country is both Belgique and Luxembourg at once, the result is always true!

What you actually wanted to say was "if $country is not Belgique and also $country is not Luxembourg, then evaluate to true": ($country <> "Belgique") and ($country <> "Luxembourg")

So:

if ( ($country <> "Belgique") and ($country <> "Luxembourg") and ($country <> "France") and ($country <> "Pays-bas") )

(This fact that not ( A or B ) is equivalent to ( not A ) and ( not B ) is known as De Morgan's Law.)


As a side note, in PHP, and and or are not quite the same as && and ||. See stackoverflow.com/questions/4502092/php-and-or-keywords

Although it doesn't make any difference in this case, because you've grouped things neatly with parentheses, && and || are more often the operators you want, giving:

if ( ($country <> "Belgique") && ($country <> "Luxembourg") && ($country <> "France") && ($country <> "Pays-bas") )

<> is identical in meaning to !=, but there is also the stricter !==, see How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ? and Is there a difference between !== and != in PHP?

So you might actually want this:

if ( ($country !== "Belgique") && ($country !== "Luxembourg") && ($country !== "France") && ($country !== "Pays-bas") )

Compare multiple values to multiple columns in database with PHP and MySQL?

This should do the trick:

SELECT * FROM table_name
WHERE ? IN (column1,column2,column3,column4,column5)
OR ? IN (column1,column2,column3,column4,column5)
OR ? IN (column1,column2,column3,column4,column5)
OR ? IN (column1,column2,column3,column4,column5)
OR ? IN (column1,column2,column3,column4,column5)

and in your PHP you'd do this:

$sth = $pdo->prepare( "the query above" );
$sth->execute( array( $var1, $var2, $var3, $var4, $var5 ) );

(assuming you use PDO).

Comparing multiple values against a MySQL table

You are looking for the very basic SELECT query with WHERE clause:

SELECT * FROM `tbl` WHERE `username` = X AND `password` = Y

I really advise you to learn SQL



Related Topics



Leave a reply



Submit