php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78530 ternary operator not working properly
Submitted: 2019-09-12 20:13 UTC Modified: 2019-09-12 20:21 UTC
From: sinigagliaalberto at gmail dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 7.2.22 OS: Ubuntu 19.04 with Apache 2.4.38
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: sinigagliaalberto at gmail dot com
New email:
PHP Version: OS:

 

 [2019-09-12 20:13 UTC] sinigagliaalberto at gmail dot com
Description:
------------
Copied by a github laravel issue (https://github.com/laravel/framework/issues/29963)
Note: 
filled(null) return false
inside the foreach you can use any collections you want,it doesn't change anything
old('something') is like $_REQUEST('something')
{{ ... }} means <?php ... ?>
@foreach ... @endforeach means <?php foreach(){...} ?>
### Description:
I'm trying to set a select option on a specific element choosen by if is in old(), if not, if belongs to the element (in this case, wine) with ternary opertor, but this 
```php
@foreach (App\Models\Region::all() as $reg)
     <option value="{{$reg->region_id}}" {{filled(old('region_id'))&&old('region_id')==$reg->region_id ?'selected': filled($wine->region)? $reg->region_id==$wine->region->region_id? "selected": '' :''}}>{{$reg->region_id}} - {{$reg->region_name}}</option>
 @endforeach
```
that formatted i belive that has this meaning 
```php
filled(old('region_id'))&&old('region_id')==$reg->region_id
    ?
        'selected'
    : 
        filled($wine->region)
            ? 
                $reg->region_id==$wine->region->region_id
                    ? 
                        "selected"
                    : 
                '' 
            :
                ''
```
is evaluated differently respect of this
```php
@foreach (App\Models\Region::all() as $reg)
          <option value="{{$reg->region_id}}" {{filled(old('region_id'))&&old('region_id')==$reg->region_id ?'selected': (filled($wine->region)? ($reg->region_id==$wine->region->region_id? "selected": '' ):'')}}>{{$reg->region_id}} - {{$reg->region_name}}</option>
@endforeach
```
that formatted i belive that has this meaning 
```php
filled(old('region_id'))&&old('region_id')==$reg->region_id 
    ?
        'selected'
    : 
        (
            filled($wine->region)
                ? 
                    (
                        $reg->region_id==$wine->region->region_id
                            ? 
                                "selected"
                            : 
                                '' 
                    )
                :
                    ''
        )
```
the difference is that the first one, also if the second filled ( `filled($wine->region)` ) returns false, runs the `$reg->region_id==$wine->region->region_id` and the second one, the one with the brackets, no

Tell me if you need more code or more information, there is no problem
Thanks, Alberto

The point is that if $wine->region does not exist, when it trys to execute `$wine->region->region_id` it throw an exception because i'm reading a property of a null element, but if `$wine->region` was null, `filled($wine->region)` should has retuned false, and so that code should not have been executed

Actual result:
--------------
descrited at the end of the bug description

Patches

There_is_no_patch (last revision 2019-09-12 20:16 UTC by sinigagliaalberto at gmail dot com)

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-12 20:15 UTC] sinigagliaalberto at gmail dot com
The following patch has been added/updated:

Patch Name: There_is_no_patch
Revision:   1568319352
URL:        https://bugs.php.net/patch-display.php?bug=78530&patch=There_is_no_patch&revision=1568319352
 [2019-09-12 20:16 UTC] sinigagliaalberto at gmail dot com
The following patch has been added/updated:

Patch Name: There_is_no_patch
Revision:   1568319394
URL:        https://bugs.php.net/patch-display.php?bug=78530&patch=There_is_no_patch&revision=1568319394
 [2019-09-12 20:18 UTC] requinix@php.net
-Status: Open +Status: Wont fix
 [2019-09-12 20:18 UTC] requinix@php.net
Nested ternaries without parentheses is deprecated since PHP 7.4 and will be an error in PHP 8.
https://wiki.php.net/rfc/ternary_associativity
 [2019-09-12 20:20 UTC] kalle@php.net
-Status: Wont fix +Status: Feedback
 [2019-09-12 20:20 UTC] kalle@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves.

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external
resources such as databases, etc. If the script requires a
database to demonstrate the issue, please make sure it creates
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.

Please avoid using a third party library or similar and minimize the script as much as possible.
 [2019-09-12 20:21 UTC] kalle@php.net
-Status: Feedback +Status: Wont fix
 [2019-09-12 20:21 UTC] kalle@php.net
Disregard that, requinix@ beat me to it
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC