Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14571

Re: Do you use extra parenthesis for include/require/echo?

From Thomas 'PointedEars' Lahn <PointedEars@web.de>
Newsgroups comp.lang.php
Subject Re: Do you use extra parenthesis for include/require/echo?
Date 2014-11-13 22:03 +0100
Organization PointedEars Software (PES)
Message-ID <3885988.HPz9jOa4Ux@PointedEars.de> (permalink)
References <87bnocvs5m.fsf@ahungry.com> <87ppcruxqz.fsf@bsb.me.uk>

Show all headers | View raw


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

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:

$ 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”.

<http://php.net/include>

> If you leave out the ()s the real parse becomes a much more obvious
> possibility:
> 
>   include "x.php" ? "all good" : "failed to include file"

I agree that the parentheses should be omitted when the include statements 
are _not_ used as a function.  But not to hide a bug.
 
-- 
PointedEars
Zend Certified PHP Engineer
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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