0

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?

Community
  • 1
  • 1

1 Answers1

0

I think this will work with any php function, be it user or inbuilt. Because the way the script goes through compiling phase, it will try to resolve the variable name and then it will make a function call with the name of the function.

Ritesh
  • 1,809
  • 1
  • 14
  • 16
  • At work I have spent days building a script that would benefit from this technique....I would just like to see somewhere in writing that it can be used on any PHP built-in function – The One and Only ChemistryBlob Dec 10 '15 at 17:08
  • 1
    See http://php.net/manual/en/language.types.callable.php : A PHP function is passed by its name as a string. Any BUILT-IN or user-defined function can be used, except language constructs such as... – maxhb Dec 10 '15 at 17:11
  • I do not know whether it is documented or not, but what you can do to is to debug the php binary with any sample code and if you understand how php compiles to create opcodes and how it execute the opcodes, then you can convince yourself that there is not difference between builtin and inbuilt function as far this question is concerned. – Ritesh Dec 10 '15 at 17:11
  • @Ritesh...I appreciate your comment but I have a mountain of things to finish at my understaffed workplace and any testing I can avoid saves me valuable time – The One and Only ChemistryBlob Dec 10 '15 at 17:18
  • @maxhb...perfect. Thanks! But this information is not made obvious to find :) – The One and Only ChemistryBlob Dec 10 '15 at 17:19