Mayron
Mayron

Reputation: 345

My code didn't work correctly with if ||

Here is my code

<?php

if( $country==("United Stats" || Canada || United Kingdom) ) {
   echo $Country;
}

?>

I want to say "if Country == United Stats 'or' Canada 'or' United Kingdom >>> echo $Country

When i use || it not work correctly What is the wrong please ?

Upvotes: 3

Views: 206

Answers (7)

GoTo
GoTo

Reputation: 6716

<?php

$country = "Canada";
if( $country=="United Stats" || $country=="Canada" || $country=="United Kingdom" ) {
   echo $country;
}

?>

Upvotes: -1

Carsten
Carsten

Reputation: 18446

Your code is not valid PHP syntax. This one will work:

<?php

if( $country == "United Stats" || $country == "Canada"
        || $country == "United Kingdom" ) {
    echo $country;
}

?>

By the way, you seem to be new to PHP. You should read the documentation and maybe a book to learn the language.

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191779

if (in_array($country, array('United Stats', 'Canada', 'United Kingdom'))) {

Upvotes: 3

AMayer
AMayer

Reputation: 355

<?php
$cs = array('United States', 'Canada', 'United Kingdom');
if(in_array($country, $cs)){
echo $country
}
?>

Upvotes: 4

J Max
J Max

Reputation: 2381

you have to compare $country to each variable

if ($country == "United States" ||
    $country == "Canada" ||
    $country == "United Kingdom") {

    echo $country;
}

Upvotes: 2

j08691
j08691

Reputation: 207943

<?php
if( $country=="United Stats" Or $country=="Canada" Or $country=="United Kingdom" ) {
   echo $country;
}
?>

Or

<?php
if( $country=="United Stats" || $country=="Canada" || $country=="United Kingdom" ) {
   echo $country;
}
?>

Upvotes: 1

cichy
cichy

Reputation: 10644

You can't do that, you need to write:

if( $country=="United Stats" || $country=="Canada" || $country=="United Kingdom" ) {

Upvotes: 4

Related Questions