简体   繁体   中英

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. 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.

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

You could of course write your own implementation of 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. I suppose it would depend on how long the string is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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