Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #14576
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Do you use extra parenthesis for include/require/echo? |
| Date | 2014-11-13 23:18 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <87sihm8zb7.fsf@bsb.me.uk> (permalink) |
| References | <87bnocvs5m.fsf@ahungry.com> <87ppcruxqz.fsf@bsb.me.uk> <3885988.HPz9jOa4Ux@PointedEars.de> |
Thomas 'PointedEars' Lahn <PointedEars@web.de> writes:
> Ben Bacarisse wrote:
>
>> I don't use the parentheses. There are some traps to using them that
>> are harder to fall into if you never do. For example, the expression
>>
>> include("x.php") ? "all good" : "failed to include file"
>>
>> does not parse as if include is a function.
>
> (Your examples are missing a semicolon to be proper PHP code. Semicolons
> are not optional in PHP, and there is no ASI.)
I deliberately wrote an expression and not a statement.
> As the “include” statement is part of an expression here, it should parse
> like a function call. I consider it a bug that it does not:
It is, however, documented behaviour (which in no way means you should
not consider it a bug). I just think it's one of several places where
PHP's syntax is a little surprising.
> $ php -r 'include("x.php") ? "all good" : "failed to include file";'
> PHP Warning: include(all good): failed to open stream: No such file or
> directory in Command line code on line 1
> PHP Stack trace:
> PHP 1. {main}() Command line code:0
> PHP Warning: include(): Failed opening 'all good' for inclusion
> (include_path='.:/usr/share/php:/home/pelinux/LCARS/scripts/php') in Command
> line code on line 1
> PHP Stack trace:
> PHP 1. {main}() Command line code:0
>
> $ php -v
> PHP 5.6.2-1 (cli) (built: Oct 17 2014 16:36:45)
> Copyright (c) 1997-2014 The PHP Group
> Zend Engine v2.6.0, Copyright (c) 1998-2014 Zend Technologies
> with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2014, by Zend
> Technologies
> with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans
>
> When the “include” statement is RHS of an assignment (i.e., in expression
> context), it is parsed like a function call, and it has a return value that
> is assigned. Likewise for “include_once”, “require”, and
> “require_once”.
I wouldn't say that it is parsed like a function call in any context.
The syntax is always
T_INCLUDE expr
so provided the expr is bracketed, and is not followed by anything that
can be included in the parse of expr, then I'd say that is *looks* like
a function call. Function call syntax binds very tightly, so the "+ 1"
in
$v = strlen("abc") + 1;
does not disrupt the parse of the function call. In contrast
$v = include("abc") + 1;
is parsed very differently.
<snip>
--
Ben.
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Do you use extra parenthesis for include/require/echo? Matthew Carter <m@ahungry.com> - 2014-11-12 01:39 -0500
Re: Do you use extra parenthesis for include/require/echo? "Christoph M. Becker" <cmbecker69@arcor.de> - 2014-11-13 00:33 +0100
Re: Do you use extra parenthesis for include/require/echo? Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-12 22:32 -0500
Re: Do you use extra parenthesis for include/require/echo? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-13 11:48 +0000
Re: Do you use extra parenthesis for include/require/echo? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2014-11-13 22:03 +0100
Re: Do you use extra parenthesis for include/require/echo? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-11-13 23:18 +0000
Re: Do you use extra parenthesis for include/require/echo? Jerry Stuckle <jstucklex@attglobal.net> - 2014-11-13 21:08 -0500
Re: Do you use extra parenthesis for include/require/echo? crankypuss <crankypuss@nomail.invalid> - 2014-11-14 02:15 -0700
csiph-web