PHP - 创建函数问题

发布于 2024-10-17 16:46:29 字数 479 浏览 2 评论 0原文

我试图理解这段代码,但我不能:(

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

自在安然 2024-10-24 16:46:29

代码可以大大简化:

$what = create_function('$a', "return \"LIMIT $limit\";");

$what = create_function('$a', 'return "LIMIT ' .  $limit .'";');

代码应该可以工作。请注意,在 SQL 语法中,LIMIT 后面的数字不得用引号引起来。

但是当您创建函数时,您也可以将 $limit 作为参数传递给函数:

$what = create_function('$limit', 'return "LIMIT $limit";');
$str = $what(10);

或者根本不使用 create_function ,而直接进行字符串连接:

$str = 'LIMIT ' . $limit;

The code can be much simplified:

$what = create_function('$a', "return \"LIMIT $limit\";");

or

$what = create_function('$a', 'return "LIMIT ' .  $limit .'";');

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:

$what = create_function('$limit', 'return "LIMIT $limit";');
$str = $what(10);

or don't use create_function at all and just do string concatenation directly:

$str = 'LIMIT ' . $limit;
空城旧梦 2024-10-24 16:46:29

当您连接字符串时,您的代码看起来有点混乱,请尝试一种更简单的方法:

create_function('$a', sprintf('return "LIMIT %d"',$limit));

如果您不介意我问,为什么要创建一个函数来返回一个简单的字符串?

Your code seems a little messy when your concatenating strings, try a simpler approach:

create_function('$a', sprintf('return "LIMIT %d"',$limit));

if you don't mind me asking, why are you creating a function to return a simple string ?

爱的十字路口 2024-10-24 16:46:29

我刚刚测试了你的第二个代码,它可以正常工作,正确传入 $limit

<?php
$limit = 10;
$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');
echo $what(2);// note: `2` was randomly chosen, and used because $what expects an argument
// result: ` LIMIT '10'`
?>

上面的 $what 行可以重写为:

$what = create_function('$a', 'return "' . " LIMIT '$limit'" . '";');

请注意,你不会意外地编写它as(请注意 $limit 周围的引号字符:

$what = create_function('$a', 'return "' . ' LIMIT "$limit"' . '";');

在这种情况下,$limit 不会被 10 替换($ 的值限制)。

I have just tested your second code, and it works, correctly passing in $limit:

<?php
$limit = 10;
$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');
echo $what(2);// note: `2` was randomly chosen, and used because $what expects an argument
// result: ` LIMIT '10'`
?>

The above $what line can be rewritten as:

$what = create_function('$a', 'return "' . " LIMIT '$limit'" . '";');

Beware that you do not accidentally write it as (note the quote characters around $limit:

$what = create_function('$a', 'return "' . ' LIMIT "$limit"' . '";');

In that case, $limit does not get substituted by 10 (value of $limit).

涙—继续流 2024-10-24 16:46:29

首先,我想知道你为什么使用 create_function ?如果有任何机会(通常有),您应该尽量避免它,因为这通常会导致非常草率的代码。

我注意到的第一件事是,您的第二个示例没有对 $a 执行任何操作:

$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');

可能应该是:

$what = create_function('$a', 'return $a . '.'"'." LIMIT '$limit'".'"'.';');

另外,在连接字符串时,请尝试将 括起来。 通过空格。它将使您的代码更具可读性(因此可调试)。最后,简单地使用双引号和单引号:)

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:

$what = create_function('$a', 'return '.'"'." LIMIT '$limit'".'"'.';');

Should probably be:

$what = create_function('$a', 'return $a . '.'"'." LIMIT '$limit'".'"'.';');

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 :)

隔纱相望 2024-10-24 16:46:29

在您的示例中,很难阅读和区分 '"

试试这个:

$what = create_function('$a', "return \"LIMIT $limit;\""); 

以及

$what = create_function('$a', 'return \'LIMIT $limit;\''); 

Or

$what = create_function('$a', 'return "LIMIT '.$limit.';"'); // most clear, I think

您可以使用 \ 作为转义字符,这意味着如果该字符之后的下一个字符是当前引号字符,则不会将其解释为结束引号

示例:

echo '\''; // will output '
echo '\"'; // will output \"
echo "\""; // will output "

It is hard to read and to difference between ' and " in your example.

Try this:

$what = create_function('$a', "return \"LIMIT $limit;\""); 

As well as

$what = create_function('$a', 'return \'LIMIT $limit;\''); 

Or

$what = create_function('$a', 'return "LIMIT '.$limit.';"'); // most clear, I think

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:

echo '\''; // will output '
echo '\"'; // will output \"
echo "\""; // will output "
疧_╮線 2024-10-24 16:46:29

在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."').

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文