I know for user-defined functions I can manipulate strings to selectively call functions:
<?php
function printName() {
return "Zack";
}
function printAge() {
return "24";
}
$functionID = "Age";
$function = "print" . $functionID;
echo $function();//returns 24
I didn't realize you could do this with built-in PHP functions...I just tried this with strpos
and strrpos
:
<?php
$x = "r";
$string = "abbabb";
$function = "str" . $x . "pos";
echo $function($string, "a"); //outputs 3 whereas if $x = ""; output is 0
I checked here and here for documentation about this. The latter link shows how to do this with user-defined functions which I already knew.
Is this technique applicable to any built-in PHP function and if so where is this documented?