0

I was learning php, but I am wondering why I can only omit the parentheses for 'echo' as other functions should not omit the parentheses.

echo "test <br>";
echo("test <br>");
$a = sin 4; //ERROR
$a = sin(4);

2 Answers2

3

Check the documentation, which comments that echo isn't actually a PHP function:

echo is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo, the parameters must not be enclosed within parentheses.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Would be great to know what was the design decision behind making it a language construct. Python's `print` was also language construct/statement before Python 3, but function after that. Also found an question that also wanted to know the why, but it seems that there's no good answer there - https://stackoverflow.com/questions/3254327/php-what-are-language-constructs-and-why-do-we-need-them – Hendrik Nov 27 '20 at 07:37
  • @Hendrik Off the top of my head, the creators of PHP perhaps wanted their `echo` to bear some resemblance to the `echo` found in Linux, which also is a language construct. – Tim Biegeleisen Nov 27 '20 at 07:44
0

echo — Output one or more strings

  
  echo ('foor','bar','ba'); //will not work
  echo ('foo'); //will work 
   

but

 echo 'foo','bar','ba'; //will work 

can also be used for brackets type casting in PHP, for example


 $string="63";
 $number=(int)$string;
 
 echo  gettype($number)."\n"; //integer
 echo  gettype($string)."\n"; //string

For this reason, the parentheses might surprise you because parentheses are not only used in functions, for example, I gave a typecast example.In addition, parentheses are used in mathematical operations, dynamic scripting languages ​​can also be such things. Not like static scripting languages

dılo sürücü
  • 3,821
  • 1
  • 26
  • 28