在php中,是否“返回false”?在echo语句之后做什么? [英] In php, does "return false" do anything after echo statement?

查看:63
本文介绍了在php中,是否“返回false”?在echo语句之后做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在php中显示错误消息时,通常是这样做的,

When I display an error message in php, I usually do it like this,

if($query){
    // Success code
}else{
    // Error message
    $error_msg = "An error occurred.";
    return false;
}

我回声 $ error_msg 前端html页面中的变量。
我只是想知道在变量实际执行任何操作后,返回假还是将它放在那里毫无意义?

and I echo $error_msg variable in the front-end html page. I am just wondering if that return false after the variable actually does anything, or is it just pointless to put it there?

我正在尝试优化代码,只是想确保!

I am trying to optimize my code and just wanted to make sure! Thanks a lot in advance!

推荐答案

是的,它告诉函数停止执行并返回值 false。这样就可以了。

Yes, it tells the function to stop executing, and to return a value "false". So it does something. Whether or not is does something USEFUL depends on your programming.

如果回显后没有可执行行,并且不需要该函数返回值,则它是否有用?

If you have no executable lines after the echo, and the function is not required to return a value, then it won't actually do anything useful.

如果确实使读者更清楚这是一个停止/失败点。有一天,您可能想要实际捕获它是否起作用或失败-因此,将其保留会更加清楚。您也可以扩展功能而无需考虑,并且需要对收益进行重新调整-再次,将其保留在其中将使其变得更容易。

If does make it clearer to readers that "this is a stop/failure point" though. And one day you might want to actually trap if it worked or failed - so leaving it in makes it clearer. You also may extend the function without thinking and need to retro-fit the returns - again, leaving it in makes it easier.

相反,在C / Assembler时代,还有一种古老的编程风格,即所有功能只有一个入口和一个出口点,以帮助进行垃圾收集。 PHP并不是必需的,但是这种样式确实使它变得整洁。在这种情况下,请设置返回值(如果需要)并在最后返回。

On the converse, there is also the old programming style from C / Assembler days that you only have one entry and one exit point for all functions to help with garbage collection. Not a requirement with PHP, but that style does make it nice and neat. In this case, set the return value (if required) and return at the end.

因此,选择适合您的样式的-但请提前考虑。使所有内容原始且具有极简主义(即,因为您严格不需要而放弃这条线)可能并不总是最佳方法。

So, go with which suits your style - but think ahead. Making everything pristine and minamlistic (i.e. dropping the line as you don't strictly need it) may not always be the best approach.

这篇关于在php中,是否“返回false”?在echo语句之后做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆