简体   繁体   English

我可以将变量的引用传递给定义为采用普通变量的函数吗?

[英]Can I pass a reference to a variable into a function defined to take a normal variable?

I need to use the PHP function strtolower($str) to put a large string in all-lower-case.我需要使用 PHP 函数strtolower($str)将大字符串放入全小写。 Since the string is large, I would have liked if it could be taken as a reference and operated on.由于字符串很大,我希望它可以作为参考并进行操作。 For example (though this doesn't work):例如(虽然这不起作用):

I wanted to do:我想做:

$myLargeString = PopulateString();
strtolower(&$myLargeString);

instead of:代替:

$myLargeString = PopulateString();
$myLargeString = strtolower($myLargeString);

is there any trick that would let me lower-case a string by reference to save some speed, or maybe another function I should be looking at?有没有什么技巧可以让我通过引用小写字符串以节省一些速度,或者我应该查看另一个函数? I'm not particularly strong in PHP so I'm not sure what to look for.我在 PHP 方面不是特别强,所以我不确定要寻找什么。

You cannot force a non-reference taking function to take a reference.您不能强制非引用获取函数获取引用。 In fact, specifying that it's pass by reference at call time is now deprecated ( http://php.net/manual/en/language.references.pass.php ).事实上,现在不推荐在调用时指定它是通过引用传递的( http://php.net/manual/en/language.references.pass.php )。

You could of course write your own implementation of strtolower().您当然可以编写自己的 strtolower() 实现。 As it would be in PHP and the strtolower implementation is in C, I'm not sure how much performance you could actually gain.就像在 PHP 中一样,而 strtolower 实现在 C 中,我不确定您实际上可以获得多少性能。 I suppose it would depend on how long the string is.我想这取决于字符串的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM