开发者

In PHP, why wasn't echo implemented as a function? (not echo vs. printf)

开发者 https://www.devze.com 2023-01-13 09:57 出处:网络
I\'m just curious. In PHP, why wasn\'t echo implemented as a funct开发者_如何学编程ion? Why didn\'t PHP just give us printf and never tell about echo? Please note that:

I'm just curious. In PHP, why wasn't echo implemented as a funct开发者_如何学编程ion? Why didn't PHP just give us printf and never tell about echo? Please note that:

  • This is not a question about echo vs. printf.
  • I already knew that echo is a language construct.

UPDATE: By the way, was printf implemented using echo?


Echo is not a function and it doesn't return a value like print. Print is a language construct too - does not require parenthesis.

Manual: echo - No value is returned. print - Returns 1, always.

The fact remains that returning a value degrades system performance.

So.. now since printf IS a function (which returns the length of the outputted string) the answer I believe is obvious.


Echo is a language construct. Function use language construct to do their job. Explaining is not exactly my specialty, but a google action brought me to this topic:

What is the difference between a language construct and a "built-in" function in PHP?

Some important content:

...

This is the root of why you can't redefine language constructs like echo or print: they're effectively hardcoded into the parser, whereas functions are mapped to a set of language constructs and the parser allows you to change that mapping at compile- or runtime to substitute your own set of language constructs or expressions.

...


Just a wild guess, but perhaps it's because PHP used to exist as CGI binaries. So it would be for making porting shell scripts easier, since you could use the echo binary in those.

0

精彩评论

暂无评论...
验证码 换一张
取 消