<?php
3;
What’s such statement for in PHP?
I can’t come up with one case that this is useful…
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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:
3is a valid expression (you couldn’t use it, e.g. in$x < 3, if it wasn’t).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.