gpt4 book ai didi

php - 多次调用一个方法,有没有更有效的方法呢?

转载 作者:行者123 更新时间:2023-12-02 05:38:11 25 4
gpt4 key购买 nike

我有一个用户注册脚本。在一个阶段我调用一个方法三次。一旦检查该方法是否返回 true,否则如果没有,它是否返回一个字符串(包含错误消息),如果它确实获取返回的字符串并将其放在一个变量中。

这样做是否更有效率,我只需要调用该方法一次?但仍能得到我需要的所有答案?

代码如下:

//check thumbnail is present and good
if($register->checkThumb()){
//send image to permanent image directory
$register->moveUploadedImage();

//if the thumbnail failed validation put the error message in variable
}else if(is_string($register->checkThumb())){
$message = $register->checkThumb();

}

最佳答案

    $thumb = $register->checkThumb(); //call method once and save in variable
/* using just if($thumb) would return always true, because
the function may returns an errormessage on failure
which is ja string, which is not empty, not 0, not false == true */
if($thumb === true){
//send image to permanent image directory
$register->moveUploadedImage();
}else{ //so then it's enough to ask for error this way
$message = $thumb;
}

关于php - 多次调用一个方法,有没有更有效的方法呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11383228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com