WolwX
WolwX

Reputation: 131

How to check php value equal or with differents conditions

I want to solve this little problem :

if($risque >= '1' && $lang !== 'fr' && ($country !== 'Spain' || 'France'))
        $msg_error = 'error';

My goal is to check if the risque is equal or superior to 1 and if lang is different than fr, and if country is different than Spain or France

So if risque is 0, no msg_error, if risque is 1 and lang = fr but country is Belgium, so show the msg_error

I'm not sure with the priority and the code !== and separator ...

Thanks

Upvotes: 0

Views: 61

Answers (6)

mamdouh alramadan
mamdouh alramadan

Reputation: 8528

Try this:

if($risque >= '1' && $lang !== 'fr' && (!in_array($country,array('Spain','France')))

Using in_array() would solve your problem to check for multiple values.

Upvotes: 2

fnightangel
fnightangel

Reputation: 426

I guess this should do:

if($risque >= 1 && $lang == 'fr' && ($country != 'Spain' || $country != 'France')){
    $msg_error = "error";
}

Notice that I remove "'" from $risque and compare $lang using ==

Upvotes: -1

thecodeparadox
thecodeparadox

Reputation: 87073

if( $risque >= '1' 
    && $lang !== 'fr' 
    && $country !== 'Spain' 
    && $country !== 'France' ) 
{
  // Code
}

Upvotes: 0

MrByte
MrByte

Reputation: 1083

Your if statement should be written this way:

if($risque >= 1 && $lang != "fr" && $country != "Spain" && $country != "France")
    $msg_error = "error";

When you are comparing an Integer value (in $risque), always remove the "'" since "'" refers to a string / character instead of refering to an Integer value.

Upvotes: 0

sunshinejr
sunshinejr

Reputation: 4854

if($risque >= 1 && $lang != "fr" && $country != "Spain" && $country != "France")

Upvotes: 0

AbraCadaver
AbraCadaver

Reputation: 78994

You need to repeat the evaluation of $country and you will need the && because if the first condition fails then the || will not be evaluated:

if($risque >= 1 && $lang !== 'fr' && ($country !== 'Spain' && $country !== 'France')) {
        $msg_error = 'error';
}

Upvotes: 0

Related Questions