Why does the name concatenation function not work in PHP?

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

gives

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

Why?

+4
source share
3 answers

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

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

Instead, you can do this:

 function my_echo($s) { echo $s; } $a = "my_echo"; $a("Huh?"); 
+17
source

echo is a language construct, not a function. What you are trying to do will work with the 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'); ?> 
+3
source

echo , print , die , require , require_once , include , include_once and others (I'm sure I missed some) are not functions, but language constructs. Using say echo() with parentheses means syntactic sugar.

If you want to use them, as you already above, you will need to wrap them in a function:

 <?php function echoMyEcho($str){ echo $str; } $c = "echoMyEcho"; $c("Let go of my eggo"); 
+1
source

All Articles