【问题标题】:Using an if else statement for two functions对两个函数使用 if else 语句
【发布时间】:2016-11-14 02:20:43
【问题描述】:

非常感谢您对这段代码的帮助。我有两个单独的函数,它们都根据从页面 URL 获取的 ID 检查数据库中的表中的数据。在显示信息时,我想使用 IF ELSE 语句来检查其中任何一个函数是否有结果,如果没有结果,则不发布任何内容,如果有结果,则发布结果。目前我有这个代码:

    <?php
    if (getArtistsBySongId($id) == NULL AND getGroupsBySongId($id) == NULL) {

    } else {
        include 'songs/getsongcredits.php';
    }
    ?>

虽然代码可以显示我的数据,但它似乎忽略了我的 IF 语句,只是发布了包含中的内容。有人可以让我知道执行此 if else 语句的正确方法吗?

谢谢,米奇

【问题讨论】:

    标签: php function if-statement null include


    【解决方案1】:

    您需要适当的外壳:

    <?php
    if ( ( getArtistsBySongId( $id ) == NULL ) AND ( getGroupsBySongId( $id ) == NULL ) ) {
      // do something
    } else {
        include 'songs/getsongcredits.php';
    }
    ?>
    

    【讨论】:

      【解决方案2】:

      你应该拒绝 if 语句,不要使用多余的 if/else。

      if ((getArtistsBySongId($id) != NULL) OR (getGroupsBySongId($id) != NULL)) {
           include 'songs/getsongcredits.php';
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-01-21
        • 2022-11-30
        • 1970-01-01
        • 1970-01-01
        • 2021-11-14
        • 1970-01-01
        相关资源
        最近更新 更多