问题描述
嘿,我正在尝试编写一个自定义断言函数,如果断言失败,将使用
将用户转发到错误页面。以下是此函数的
定义及其预期用法示例:
函数fm_assert($ iserror)
{
if($ iserror)
{
header(" Location:http://x.com/errorpage.php");
返回true;
}
else
返回false;
}
// ========
//使用示例
// ======= =
fm_assert(!mysql_query($ sql));
....
header(" Location:http:// second -page.com");
这里应该发生的是如果有错误(断言失败),那么
用户应转发到错误页面
(不是错误页面
(这是我想的,因为标题位置是由第二个标题调用替换为
)。
为了防止这种情况,我将这些返回语句添加到fm_assert
函数中,并开始像这样使用它:
if (fm_assert(!mysql_query($ sql)))退出;
这很好用,但看起来很难看。我的问题是(终于)是否有某种方式使这个更光滑,所以我可以将断言函数称为
fm_assert($ iserror)而不必将在'
中''''或''退出''在那里?
PS-我试图将出口移到fm_assert,但很明显只有
退出fm_assert函数本身,而不是它运行的页面
in。
谢谢!
Hey, I am trying to write a custom assert function that would be used
to forward a user to an error page if the assertion fails. Here is the
definition of this function and an example of its intended usage:
function fm_assert($iserror)
{
if($iserror)
{
header("Location:http://x.com/errorpage.php");
return true;
}
else
return false;
}
//========
// USAGE EXAMPLE
//========
fm_assert(!mysql_query($sql));
....
header("Location: http://second-page.com");
What should happen here is if there IS an error (assertion fails), then
the user should be forwarded to the error page
(http://x.com/errorpage.php). However, what DOES happen is that the
user actually ends up at http://second-page.com not the error page
(this is happening I guess because the header Location is being
replaced by the 2nd header call).
To prevent this, I added those return statements to the fm_assert
function, and started using it like this:
if(fm_assert(!mysql_query($sql))) exit;
This works fine, but it looks ugly. My question is (finally) Is there
some way to make this sleeker so I can call the assert function as just
fm_assert($iserror) and not have to put the ''if'' or the ''exit'' in
there?
PS- i tried to move the exit to the fm_assert, but obviously that only
exited the fm_assert function itself, not the page that it was running
in.
Thanks!
这篇关于编写自定义断言函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!