php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #61529 Make unset() an expression
Submitted: 2012-03-27 13:45 UTC Modified: 2013-02-18 00:35 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: asserte at gmail dot com Assigned:
Status: No Feedback Package: Scripting Engine problem
PHP Version: 5.4.1RC/5.5.0-dev OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2012-03-27 13:45 UTC] asserte at gmail dot com
Description:
------------
isset() && unset()

PHP Parse error:  syntax error, unexpected 'unset' (T_UNSET), expecting ')' in 
/tmp/isset.php on line 4

Tested on PHP 5.4.0-3 (cli) (built: Mar 22 2012 07:59:57) 


Test script:
---------------
$> cat /tmp/isset.php
  1 <?php
  2     
  3     $entity = array('id' => 1, 'name' => 'example');
  4     isset( $entity['id'] ) && unset( $entity['id'] );

$> php /tmp/isset.php
PHP Parse error:  syntax error, unexpected 'unset' (T_UNSET), expecting ')' in /tmp/isset.php on line 3

Expected result:
----------------
unset should works.


Patches

bug61529.phpt (last revision 2012-03-29 12:51 UTC by laruence@php.net)
bug61529.patch (last revision 2012-03-29 12:49 UTC by laruence@php.net)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-03-27 13:49 UTC] asserte at gmail dot com
-Package: *Compile Issues +Package: Scripting Engine problem
 [2012-03-27 13:49 UTC] asserte at gmail dot com
Move to SE problems
 [2012-03-28 08:51 UTC] yohgaki@php.net
Verified.

$ php -r '1 && unset($var["a"]);'

is enough.

----------
[yohgaki@dev php-src]$ ./sapi/cli/php -v
PHP 5.5.0-dev (cli) (built: Mar 28 2012 17:33:51) (DEBUG)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies

[yohgaki@dev php-src]$ ./sapi/cli/php -r 'unset($var["a"]);'
[yohgaki@dev php-src]$ ./sapi/cli/php -r '1 && unset($var["a"]);'

Parse error: syntax error, unexpected 'unset' (T_UNSET) in Command line code on 
line 1
----------
 [2012-03-28 08:51 UTC] yohgaki@php.net
-Status: Open +Status: Verified
 [2012-03-28 08:52 UTC] yohgaki@php.net
-Operating System: Debian +Operating System: Linux -PHP Version: 5.4.0 +PHP Version: 5.4.1RC/5.5.0-dev
 [2012-03-29 12:49 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug61529.patch
Revision:   1333025364
URL:        https://bugs.php.net/patch-display.php?bug=61529&patch=bug61529.patch&revision=1333025364
 [2012-03-29 12:49 UTC] laruence@php.net
-Assigned To: +Assigned To: dmitry
 [2012-03-29 12:49 UTC] laruence@php.net
Dmitry, could you review my patch for this bug plz?  thanks
 [2012-03-29 12:51 UTC] laruence@php.net
The following patch has been added/updated:

Patch Name: bug61529.phpt
Revision:   1333025478
URL:        https://bugs.php.net/patch-display.php?bug=61529&patch=bug61529.phpt&revision=1333025478
 [2012-03-29 12:53 UTC] laruence@php.net
oh, the patch is based on trunk.
 [2012-03-29 13:07 UTC] reeze dot xia at gmail dot com
unset() is like echo These are language constuct, Both of them are not 
expression in PHP, so code like this:

$c = 1 && echo 3;

IS NOT VALID script too;

Those language construct are "unticked_statement" @see 
Zend/zend_language_parser.y.
if,switch, do while and etc they all.

So from now on, This is not a bug.

Maybe we could make unset() a normal expression. I don't know how this decision 
made, maybe we can discuss it in maillist.
 [2012-03-29 13:28 UTC] nikic@php.net
unset() is a statement, not an expression. So you obviously can't use it in an expression context. Changing buy type to Feature Request.

In my eyes the behavior *should not* be changed. unset() does not have a meaningful return value and as such should not be allowed in an expression context.
 [2012-03-29 13:28 UTC] nikic@php.net
-Summary: Parsing error +Summary: Make unset() an expression -Type: Bug +Type: Feature/Change Request
 [2012-03-29 14:59 UTC] laruence@php.net
@nikic thinking of isset, include. :)
 [2012-03-29 14:59 UTC] laruence@php.net
@nikic thinking of isset, include. :)
 [2012-03-29 15:02 UTC] laruence@php.net
-Summary: Make unset() an expression +Summary: Parsing error while use unset with boolean and -Type: Feature/Change Request +Type: Bug
 [2012-03-29 15:34 UTC] laruence@php.net
@nikic @reeze, after a deep thought,  I realize that I was wrong. yes, to fix this 
we should make unset return something.. 

so, mark it as a req , 
thanks
 [2012-03-29 15:36 UTC] laruence@php.net
-Summary: Parsing error while use unset with boolean and +Summary: Make unset() an expression -Type: Bug +Type: Feature/Change Request -Assigned To: dmitry +Assigned To:
 [2012-03-30 12:12 UTC] dmitry@php.net
-Status: Verified +Status: Feedback
 [2012-03-30 12:12 UTC] dmitry@php.net
I didn't test the patch just made a quick review. I suppose it's incomplete. With patch it's possible to write $a = unset($b), but UNSET opcode doesn't care about return value, so what's going to be assigned to $a?

In any case, I think, such changes are not allowed in minor releases. So must not be committed into 5.4. Personally, I don't think it makes sense for trunk as well. It always possible to write

if (isset($entity['id'])) unset($entity['id']);

More clear, and just a few characters more :)
 [2012-05-09 15:00 UTC] jelle dot zijlstra at gmail dot com
I just hit this bug trying to unset a variable in a for loop, like this:
<?php
for( ; ; unset($i)) {
    if($someCondition) {
        $i = 42;
    }
    if(isset($i)) {
        doSomething();
    } else {
        doSomethingElse();
    }
}

I understand why this currently fails and obviously there are workarounds, but intuitively unset should work in the third expression of a for loop.
 [2013-02-18 00:35 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Sun Dec 04 16:05:54 2022 UTC