停止一个 php 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2163419/
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
Stop a php function
提问by Caio
I have this code that simply validates a date, but ends the script if a error occurs (exit ()).
我有这个代码,它只是验证一个日期,但如果发生错误,则结束脚本 ( exit ())。
Now i want to put this same code in a single function that returns falseon error.
现在我想把这个相同的代码放在一个false错误返回的函数中。
I dont want to use "if/else". I want to return falseon error and stop execution of the others things.
我不想使用“if/else”。我想返回false错误并停止执行其他事情。
Is that possible?
那可能吗?
回答by Ignacio Vazquez-Abrams
Learn exceptions. Live them. Love them.
学习异常。住他们。爱他们。
回答by Jayrox
you could change
你可以改变
exit ("Error !");
to
到
return false;
but exceptions would definitely be better as suggested by Ignacio Vazquez-Abrams
但例外肯定会更好,正如 Ignacio Vazquez-Abrams 所建议的
回答by jeroen
I see you already have your answer, but as a side note, are you aware that there is a php function that replaces a big part of your function: checkdate()
我看到您已经有了答案,但作为旁注,您是否知道有一个 php 函数替换了您的大部分函数:checkdate()
No need for regex's, if statements, etc.
不需要正则表达式、if 语句等。

