简体   繁体   中英

OR function in if (trim() == "") {}in PHP

OR function in if (trim() == "") {} in PHP?

tried this, not work:

if (trim($query) == "a" or "b") {$search= 'd'; }

I want if either a or b exist d goes to $search

Learn PHP the right way from PHP Manual . It should be:

if (trim($query) == "a" || trim($query) == "b") {
  $search= 'd';
}

Or you can use in_array :

if (in_array(trim($query), array('a', 'b'))) {
  $search = 'd';
}

If you wanna look for partial ones, then you can do:

if (strpos("a", trim($query)) > -1 || strpos("b", trim($query)) > -1) {

In your conditional, you must use the if statement 2x with or or || operator, like this:

if (trim($query) == "a" or trim($query) == "b") {$search= 'd'; }

You can also use this:

if (trim($query) == "a" || trim($query) == "b") {$search= 'd'; }

Of course, you can also use in_array() function in your statement:

if (in_array(trim($query), array('a', 'b'))) {
  $search = 'd';
}
if( in_array( trim($query), array('a', 'b') ) ) {
  $search = 'd';
}

Easy peasy

if (trim($query) == "a" or trim($query) == "b"){
   // Do somenthing
}

First of all, you probably should not use the or operator, since it has a very weird precedence. Use || instead.

http://php.net/manual/en/language.operators.logical.php

Second, you need to compare the result of trim twice. or is a simple logical operator that evaluates boolean expressions and returns a bool, it won't magically make two comparisons happen.

$query = trim(query);
if ($query === 'a' || $query === 'b') { $search = 'd'; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM