问题描述
我的代码是这样的:
<?php
define("ERROR", "SOMETHING WRONG WITH MY DATABASE");
...
if (!mysql_query($q)){
die(ERROR);
}
?>
现在我想用 mysql_error替换用我的数据库写错了 )
,以防我要调试它。最简单的方法是什么?
Now I want to replace "SOMETHING WRONG WITH MY DATABASE" with mysql_error()
in case I want to debug it. what is the easiest way ?
这似乎不起作用: define( ERROR,mysql_error());
----编辑---
---- edit ---
我不想使用mysql_error( )在生产环境下,它可能有助于攻击者找出与我的数据库有关的内容?这就是我使用常量字符串的目的
I don't want to use mysql_error() under production environment, it may help the attacker figure out something related to my database? That's my point of using a constant string
在 C 中,您可以执行
#define x yourfunction ()
我不确定是否可以在php中做同样的事情
as in C you can do#define x yourfunction()
I'm not sure if I can do the same in php
推荐答案
简单的答案是您不能那样做。 恒定的全部意义在于它是恒定,就像其值从未改变一样。如果它引用一个函数调用,则该函数可以返回任何值-而且它不再是常量。
The simple answer is "you cannot do that". The whole point of constant is that it's constant as in its value is never changed. If it refers to a function call, the function can return any value - and it's not constant any more.
您可以做的一个技巧是将函数调用本身定义为常量的值-然后按需 eval
,如下所示:
One trick you can do is define the function call itself to be the value of the constant - and then eval
it on demand, something like this:
define("ERROR", "return mysql_error()");
...
die(eval(ERROR));
但是,这确实是一个很糟糕的代码。
However this is really a rather bad code. You'd be much better off doing
die(mysql_error());
这篇关于定义一个常量函数调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!