tags:

views:

214

answers:

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

+16  A: 

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?");
Fragsworth
Just to add a precision : echo is a "language construct", and cannot be called using variable functions -- as stated by its manual page : http://php.net/echo
Pascal MARTIN
You added the function wrap while I was writing my answer, which mentioned the function wrap. +1
MitMaro
I'm going to say something I haven't said since 2001: thank you for teaching me something new about PHP.
qwzybug
+2  A: 

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');
?>
Tom
+1  A: 

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