Why in PHP string can become a function?

Question:

In the example I have a function with the name of funcao , which aims only to display the value of the parameter on the screen.

When creating a string with the name of the function and calling it as a function, it will be executed:

<?php

function funcao($parametro = "default") {
    echo $parametro . "<br/>\n" ;
}

$func = "funcao"; 
$func("kirotawa");

$func = "FUNCAO";
$func();

?>

See on Ideone

Why does it happen to be able to call the function as follows?

Answer:

the same works with variable variables, the name is kind of strange, but it exists as in the example below:

$vara = "c";
$varb = "vara";
echo $$varb;

ç

This happens because you can use the value of a variable to call functions, or access other variables, thus ensuring greater flexibility in the language.

How does PHP interpret the last line?

echo $$varb;

replaces the first variable with its value ($varb per rod);

echo $vara;

replaces the second variable with its value ($stick for c);

echo c;

and print the value on the screen

Scroll to Top