PHP - 创建函数问题
我试图理解这段代码,但我不能:(
$time = date('Y-m-d', strtotime('-30 days'));
$what = create_function('$a', 'return $a.'.'"'." AND date > '$time'".'"'.';');
为什么 $time
变量在这个创建的函数中成功传递,但是当我尝试时:
$limit = 10;
$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');
$limit
有效
ps:如果我尝试 $what = create_function('$a', 'return '.'"'." LIMIT 10".'"'.';');
它 ...
I'm trying to understand this code and I can't :(
$time = date('Y-m-d', strtotime('-30 days'));
$what = create_function('$a', 'return $a.'.'"'." AND date > '$time'".'"'.';');
Why does the $time
variable get passed successfully in this created function, but when I try:
$limit = 10;
$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');
$limit
doesn't ?
ps: if I try $what = create_function('$a', 'return '.'"'." LIMIT 10".'"'.';');
it works...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
代码可以大大简化:
或
代码应该可以工作。请注意,在 SQL 语法中,
LIMIT
后面的数字不得用引号引起来。但是当您创建函数时,您也可以将
$limit
作为参数传递给函数:或者根本不使用
create_function
,而直接进行字符串连接:The code can be much simplified:
or
The code should work. Note that the number after
LIMIT
must not be enclosed in quotes in the SQL syntax.But as you are creating a function, you could also pass
$limit
as parameter to the function:or don't use
create_function
at all and just do string concatenation directly:当您连接字符串时,您的代码看起来有点混乱,请尝试一种更简单的方法:
如果您不介意我问,为什么要创建一个函数来返回一个简单的字符串?
Your code seems a little messy when your concatenating strings, try a simpler approach:
if you don't mind me asking, why are you creating a function to return a simple string ?
我刚刚测试了你的第二个代码,它可以正常工作,正确传入
$limit
:上面的
$what
行可以重写为:请注意,你不会意外地编写它as(请注意
$limit
周围的引号字符:在这种情况下,
$limit
不会被10
替换($ 的值限制
)。I have just tested your second code, and it works, correctly passing in
$limit
:The above
$what
line can be rewritten as:Beware that you do not accidentally write it as (note the quote characters around
$limit
:In that case,
$limit
does not get substituted by10
(value of$limit
).首先,我想知道你为什么使用
create_function
?如果有任何机会(通常有),您应该尽量避免它,因为这通常会导致非常草率的代码。我注意到的第一件事是,您的第二个示例没有对
$a
执行任何操作:可能应该是:
另外,在连接字符串时,请尝试将
括起来。
通过空格。它将使您的代码更具可读性(因此可调试)。最后,简单地使用双引号和单引号:)First of all, I am wondering why you are using
create_function
? If there is any chance (and there usually is), you should try to avoid it, since this generally results in very sloppy code.The first thing I notice is that your second example doesn't do anything with
$a
:Should probably be:
Also, when concatenating strings, try surround the
.
by spaces. It will make your code more readable (and thus, debugabble). Lastly, go easy on the double and single quotes :)在您的示例中,很难阅读和区分
'
和"
。试试这个:
以及
Or
您可以使用
\
作为转义字符,这意味着如果该字符之后的下一个字符是当前引号字符,则不会将其解释为结束引号示例:
It is hard to read and to difference between
'
and"
in your example.Try this:
As well as
Or
You can use
\
as escaping character, this means the next character after this will not be interpreted as a closing quote if it is the current quote character.Example:
在create_function中,
$limit
用单引号括起来;您应该将其包含在双引号中 ("LIMIT $limit"
) 或使用串联 (.
) 运算符(如下所示:'".$limit. “'
)。In create_function,
$limit
is in single quotes; either you should include it in double quotes ("LIMIT $limit"
) or use the concatenation (.
) operator (like this:'".$limit."'
).