如果||,我的代码无法正常工作

时间:2012-07-11 20:51:06

标签: php if-statement

这是我的代码

<?php

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

?>

我想说“if Country == United Stats'或'Canada'或'United Kingdom&gt;&gt;&gt;  echo $ Country

当我使用||时它无法正常工作 有什么不对吗?

7 个答案:

答案 0 :(得分:4)

你不能这样做,你需要写:

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

答案 1 :(得分:4)

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

答案 2 :(得分:3)

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

答案 3 :(得分:2)

您必须将$ country与每个变量进行比较

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

    echo $country;
}

答案 4 :(得分:1)

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

或者

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

答案 5 :(得分:1)

您的代码不是有效的PHP语法。这个可行:

<?php

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

?>

顺便说一下,你似乎是PHP的新手。你应该阅读documentation和一本书来学习这门语言。

答案 6 :(得分:-1)

<?php

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

?>