
In PHP, and especially when using Database surgeries like mysqli works, you may wish to create dynamic variable names. Luckily PHP makes this a very mundane job to accomplish.
First let’s take a look at an example so we understand the basic idea of how to do dynamic factors. After that we can take a look at a more complex, but also more realistic illustration involving variable’s out of a database.
For our first example, we will be adding a prefix into a collection of variables. In order to utilize a variable from the variable name statement, we just surround the declaration with … .
Have a look at the next example:
<?php
$prefix = "pre_";
${$prefix . one} = "First Variable <br />";
${$prefix . two} = "Second Variable <br />";
${$prefix . three} = "Third Variable <br />";
echo $pre_one;
echo $pre_two;
echo $pre_three;
?>
How to dynamically pass value so, that the CMD command can execute different php files in a dockerfile?
I have a long dockerfile. In the end I am trying to run a php file using CMD command. This php file is different for each cluster. How can I dynamically pass the name of the php file with the path?
ARG MyFile
ENV env_var= MyFile
CMD php ${MyFile}
This isn’t working for me as during docker build I am passing the path of my php file and during docker run it throws an error that php not found.
call_user_func_arrayCall_user_func_array -- Call a callback using an array of parametersDescription ¶call_user_func_array ( callable $callback, range $param_arr ): mixedCalls the callback given by the first parameter with the parameters in param_arr.Parameters ¶callbackThe callable to be called.param_arrThe parameters to be passed to the callback, as an indexed array.
Returns the return value of the callback, or FALSE on mistake.Changelog ¶Model Guide 5.3.0The interpretation of object oriented key words such as parent and self has changed. Formerly, calling them using the double colon syntax would exude an E_STRICT warning because they were interpreted as static. Examples ¶Example #1 call_user_func_array() instance Function foobar($arg, $arg2) echo __FUNCTION__," got $arg and $arg2\n";class foo function bar($arg, $arg2) echo __METHOD__," got $arg and $arg2\n"; // Call the foobar() function with 2 argumentscall_user_func_array("foobar", array("one","2"));// Call the $foo->bar() method with 2 arguments$foo = new foo;call_user_func_array(array($foo,"bar"), array("three","four"));?
> The above example will output something like:Foobar got two and one
Foo:bar got three and four
Example #2 call_user_func_array() using namespace namenamespace Foobar;class Foo static public function test($name) print "Hello $name! \n"; // As of PHP 5.3.0call_user_func_array(__NAMESPACE__ .' \Foo::test', array('Hannes'));// As of PHP 5.3.0call_user_func_array(array(__NAMESPACE__.' \Foo','test'), array('Philip'));?> The above example will output something like:Hello Hannes!
Hello Philip!
Example #3 Using lambda function$func = function($arg1, $arg2) return $arg1 * $arg2;;var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.3.0 */?> The above example will output:int(8)
Example #4 Passing values by referenceFunction mega(&$a) $a = 55; echo"function mega \$a=$a\n";$bar =77;call_user_func_array('mega',array(&$bar));echo"international \$bar=$bar\n";?> The above example will output:Operate mega $a=55
Global $bar=55
Notes
Note:Earlier PHP 5.4, referenced factors in param_arr are passed to the function by reference, irrespective of whether the function expects the various parameter to be passed by reference. This kind of call-time pass with regard does not emit a deprecation note, but it is nonetheless deprecated, and was eliminated in PHP 5.4. Furthermore, this does not apply to inner functions, for the function signature is honored. Passing by value when the function expects a parameter by reference results in a warning and havingcall_user_func() return FALSE (there is, however, an exception for passed values with reference count = 1, such as in literals, because these can be turned into references with no ill effects -- but also without writes to that value with no effect --do not rely in this behavior, however, since the reference count is a implementation detail and the soundness of this behavior is questionable).
