如何对 PHP 中的函数执行 if 语句?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2259857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:45:46  来源:igfitidea点击:

How to do an if statement on a function in PHP?

phpfunctionif-statement

提问by Bruce

I just realized that you can't just use an if statement on a function, for example this doesn't work:

我刚刚意识到你不能只在函数上使用 if 语句,例如这不起作用:

function sayHello()
{
    echo "Hello World";
}

if(sayHello())
    echo "Function Worked";
else
    echo "Function Failed";

I also saw that a function can't be put as the value of a variable. So how can I do an if statement to check if a function has executed properly and display it to the browser?

我还看到函数不能作为变量的值。那么我如何做一个 if 语句来检查一个函数是否正确执行并将其显示给浏览器?

回答by lemon

It's not working since sayHello()doesn't return anything place return truein there or something.

它不起作用,因为sayHello()没有返回任何地方return true或其他东西。

回答by fastcodejava

if (sayHello() === FALSE)
    echo "Function Failed";
else
    echo "Function Worked";

回答by user3664663

this will work

这会起作用

<?php

$name1 = "bobi";

function hello($name) {
if($name == "bobi"){
    echo "Hello Bob";
  } else {
      echo "Morning";
    }
}

hello($name1);
?>

回答by Ugur Pekesen

function selam($isim)
{
    if ($isim == 'ugur') {
        return 'Selam '.$isim.' :)';
    }

    return 'Sen kimsin kardes?';
}

echo selam('ugur');