开发者

Why PHP allows 3; statement?

开发者 https://www.devze.com 2023-03-25 23:44 出处:网络
<?php 3; What\'s such statement for in PHP? I can\'t come up with o开发者_运维知识库ne case that this is useful...You\'re right, it\'s \"useless\" (more formally, it has no effect - the integer
<?php

3;

What's such statement for in PHP?

I can't come up with o开发者_运维知识库ne case that this is useful...


You're right, it's "useless" (more formally, it has no effect - the integer three is loaded and then discarded). It follows naturally from two facts:

  • 3 is a valid expression (you couldn't use it, e.g. in $x < 3, if it wasn't).
  • Expressions are also valid statements. Assignments are expressions, and you don't want free-standing assignments an error. And even if assignments were made statements, the next example, calls to functions which don't return a meaningful value, is still incredibly common and useful.

Disallowing statements with no effect is both hard (requires quite clever AST analysis and perhaps, for more complex cases, also some AST transformations such as constant folding to make them visible), impossible to get completely right (there will always be something that's missed out; doubly so in dynamic languages) and rarely useful.

Note that pretty much all other languages have this tradeoff - either you implement complex compiler passes to disallow it (potentially missing more subtle cases), or ignore it. This even applies to language which have only expressions to some extent, except of course that there are no statements and you'd instead call expressions whose value is ignored useless.


It's probably a case of making it not allowed would be more work than it's worth. It obviously does nothing useful, so programmers will likely avoid this kind of thing.

One thing to consider is that:

some_function("foo");

and

3;

are the syntactically the same.


It's not usefull but allowed.

Every string of code is expression in PHP as 3 too. (ex $v = 17 is exppressions with value int(17) too, print 17 is also expression with value int(1) )

So code is <?php expr;expr;expr; ?>

Why it shouldn't be allowed?


AFAIK, you can do this in JS as well as in Python, I don't know anything else than JS, PHP/Python but it should work well with most, you can safely run these in any of the three 3; or 'Hello World!';, please note that you don't need semi colon at the end of statement in Python


In a progaming language it's usefull or not is determined by what you write.

A compiler (php is not a complied lang though) will check is their is any syntex error. And it does't have a sense to understand wether it is useful or not. So for php it's valid.

IF WE WRITE THE SAME CODE IN JAVA IT WILL PRODUCE

Unresolved compilation problems: Syntax error, insert "AssignmentOperator Expression" to complete Expression The left-hand side of an assignment must be a variable

iN c# IT WILL PRODUCE

Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement

sO different language have differnt "type checking".

0

精彩评论

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