【问题标题】:php echo if two conditions are true如果两个条件为真,则 php 回显
【发布时间】:2011-08-21 03:07:00
【问题描述】:

实际代码如下所示:

if (file_exists($filename)) {echo $player;

} else { 

echo 'something';

但即使没有从 url 调用 id 也会显示播放器

我需要这样的东西:

check if $filename exists and $id it is not empty then echo $player

if else echo something else

我检查 $id 是否不为空

if(empty($id)) echo "text";

但我不知道如何将它们结合起来

有人可以帮帮我吗?

感谢您提供所有代码示例,但我仍有问题:

我如何检查 $id 不为空然后回显其余代码

【问题讨论】:

标签: php if-statement echo file-exists


【解决方案1】:
if (!empty($id) && file_exists($filename))

【讨论】:

  • 我有一个小问题:我如何检查它是否为空?
  • @m3tsys !empty($id) 转换为如果 $id 不为空。这就是你要找的答案
  • 谢谢。我在其他代码中遇到了一些问题,我认为这是由此引起的。
【解决方案2】:

只需使用AND&& 运算符来检查两个条件:

if (file_exists($filename) AND ! empty($id)): // do something

这是基本的 PHP。阅读材料:

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

http://www.php.net/manual/en/language.operators.precedence.php

【讨论】:

    【解决方案3】:

    你需要logical AND operator:

    if (file_exists($filename) AND !empty($id)) {
        echo $player;
    }
    

    【讨论】:

      【解决方案4】:
      if (file_exists($filename) && !empty($id)){
         echo $player;
      }else{
         echo 'other text';
      }
      

      【讨论】:

        【解决方案5】:

        您需要检查$idfile_exists($filename),如下所示

        if (file_exists($filename) && $id != '') {
        echo $player;
        
        } else { 
        echo 'something';
        }
        

        【讨论】:

          【解决方案6】:

          使用三元运算符:

          echo (!empty($id)) && file_exists($filename) ? 'OK' : 'not OK';
          

          使用 if-else 子句:

          if ( (!empty($id)) && file_exists($filename) ) {
              echo 'OK';
          } else {
              echo 'not OK';
          }
          

          【讨论】:

          • 您的示例中不需要所有这些括号。
          • 仅供参考。你可以像(((($this))))一样添加任意数量的东西,这并没有什么区别,它更适合像if ((1 + 1 == 2) && (3 + 3 == 6 AND 6 == 6))这样的条件分组。是的,不好的例子,但不希望评论中出现代码泛滥:)
          • 我知道。但有时添加额外的括号会改变很多 - 在 PHP 新手理解代码方面。只是想确保避免类似“!$a && $b 是否等于!($a && $b)(!$a) && $b?”的问题。 ;)
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-06
          • 1970-01-01
          • 1970-01-01
          • 2020-06-25
          • 2013-05-04
          • 2023-03-12
          相关资源
          最近更新 更多