Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't function name concatenation work in PHP?

Tags:

php

echo

<?php

$a = 'ec';
$b = 'ho';
$c = $a.$b;
echo('Huh?');
$c('Hello, PHP!');

?>

yields

Huh?
Fatal error: Call to undefined function echo() in <...>/php.php on line 11

Why?

like image 544
qwzybug Avatar asked Nov 29 '22 19:11

qwzybug


2 Answers

echo is technically not a function in PHP. It is a "language construct".

echo('Huh?') is an alternate syntax for echo 'Huh?'

You can do this instead:

function my_echo($s) {
    echo $s;
}

$a = "my_echo";
$a("Huh?");
like image 142
Fragsworth Avatar answered Dec 06 '22 16:12

Fragsworth


echo is a language construct and not a function. What you're trying to do will work with actual functions. Something like this will work.

<?php
function myecho($src) { echo $src; }
$a = 'myec';
$b = 'ho';
$c = $a.$b;
$c('This is a test');
?>
like image 39
Tom Avatar answered Dec 06 '22 16:12

Tom