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


Groups > comp.lang.php > #14545 > unrolled thread

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

Started byMatthew Carter <m@ahungry.com>
First post2014-11-12 01:39 -0500
Last post2014-11-14 02:15 -0700
Articles 8 — 6 participants

Back to article view | Back to comp.lang.php


Contents

  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

#14545 — Do you use extra parenthesis for include/require/echo?

FromMatthew Carter <m@ahungry.com>
Date2014-11-12 01:39 -0500
SubjectDo you use extra parenthesis for include/require/echo?
Message-ID<87bnocvs5m.fsf@ahungry.com>
Just a syntactic question - which combination of the above does everyone
use (or some blend of the two)?

<?php

require_once '/some/file.php';
echo 'Loaded';

?>

Or:

<?php

require_once('/some/file.php');
echo('Loaded');

?>


-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

[toc] | [next] | [standalone]


#14551

From"Christoph M. Becker" <cmbecker69@arcor.de>
Date2014-11-13 00:33 +0100
Message-ID<m40qog$gvl$1@solani.org>
In reply to#14545
Matthew Carter wrote:

> Just a syntactic question - which combination of the above does everyone
> use (or some blend of the two)?
> 
> <?php
> 
> require_once '/some/file.php';
> echo 'Loaded';
> 
> ?>
> 
> Or:
> 
> <?php
> 
> require_once('/some/file.php');
> echo('Loaded');
> 
> ?>

I avoid parens when they are neither necessary nor helpful for reading
an expression.  In these cases there aren't.

-- 
Christoph M. Becker

[toc] | [prev] | [next] | [standalone]


#14557

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-11-12 22:32 -0500
Message-ID<m418na$l57$3@dont-email.me>
In reply to#14545
On 11/12/2014 1:39 AM, Matthew Carter wrote:
> Just a syntactic question - which combination of the above does everyone
> use (or some blend of the two)?
> 
> <?php
> 
> require_once '/some/file.php';
> echo 'Loaded';
> 
> ?>
> 
> Or:
> 
> <?php
> 
> require_once('/some/file.php');
> echo('Loaded');
> 
> ?>
> 
> 

I use parents in require and include expressions, just because they take
a single parameter and I think it's a bit more clear.  I don't use them
in echo statements, because it can take more than one parameter.

However, I also admit it's a matter of style.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14559

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-11-13 11:48 +0000
Message-ID<87ppcruxqz.fsf@bsb.me.uk>
In reply to#14545
Matthew Carter <m@ahungry.com> writes:

> Just a syntactic question - which combination of the above does everyone
> use (or some blend of the two)?
>
> <?php
>
> require_once '/some/file.php';
> echo 'Loaded';
>
> ?>
>
> Or:
>
> <?php
>
> require_once('/some/file.php');
> echo('Loaded');
>
> ?>

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.  If you leave out the ()s
the real parse becomes a much more obvious possibility:

  include "x.php" ? "all good" : "failed to include file"

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#14571

FromThomas 'PointedEars' Lahn <PointedEars@web.de>
Date2014-11-13 22:03 +0100
Message-ID<3885988.HPz9jOa4Ux@PointedEars.de>
In reply to#14559
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.

[toc] | [prev] | [next] | [standalone]


#14576

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-11-13 23:18 +0000
Message-ID<87sihm8zb7.fsf@bsb.me.uk>
In reply to#14571
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.

[toc] | [prev] | [next] | [standalone]


#14578

FromJerry Stuckle <jstucklex@attglobal.net>
Date2014-11-13 21:08 -0500
Message-ID<m43o77$59p$1@dont-email.me>
In reply to#14576
On 11/13/2014 6:18 PM, Ben Bacarisse wrote:
> 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.
> 

That is true, but then PHP wasn't designed - it just kinda grew over the
years.  But it's much better now that it used to be.

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

Yes, it *looks* like a function call - but remember, expressions can
have parens, also.  The expression (2+2) is just as legal as the
expression 2+2.

What makes it look like a function call is the "include".  But despite
its text name, it's really more of a unary operator than anything else.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#14579

Fromcrankypuss <crankypuss@nomail.invalid>
Date2014-11-14 02:15 -0700
Message-ID<m44h6s$3tg$1@dont-email.me>
In reply to#14545
On 11/11/2014 11:39 PM, Matthew Carter wrote:
> Just a syntactic question - which combination of the above does everyone
> use (or some blend of the two)?
>
> <?php
>
> require_once '/some/file.php';
> echo 'Loaded';
>
> ?>
>
> Or:
>
> <?php
>
> require_once('/some/file.php');
> echo('Loaded');
>
> ?>
>
>

All of my includes are in either old-code or test-cases, and the 
"symbolic linker" that I use to build my apps parses either version; PHP 
apps that I actually use don't have any includes at all and are both 
compressed and versioned.

It really doesn't matter which form you use, quotes or parens, what 
matter is whether the code works when you run it.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web