Question

<?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?

Was it helpful?

Solution

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?");

OTHER TIPS

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');
?>

echo, print, die, require, require_once, include, include_once and others (I am sure I missed some) are not functions but language constructs. The use of say echo() with brackets is syntax sugar.

If you want to use them like you have above you will need to wrap them into a function:

<?php
function echoMyEcho($str){
    echo $str;
}

$c = "echoMyEcho";
$c("Let go of my eggo");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top