Suhyeon CHOI
Suhyeon CHOI

Reputation: 55

Why can I omit only echo's parentheses from php?

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

Upvotes: 0

Views: 283

Answers (2)

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

Upvotes: 0

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521804

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.

Upvotes: 3

Related Questions