Bug #78530 [Wfx->Fbk]: ternary operator not working properly

From: Date: Thu, 12 Sep 2019 20:20:54 +0000
Subject: Bug #78530 [Wfx->Fbk]: ternary operator not working properly
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222716@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78530&edit=1

 ID:                 78530
 Updated by:         kalle@php.net
 Reported by:        sinigagliaalberto at gmail dot com
 Summary:            ternary operator not working properly
-Status:             Wont fix
+Status:             Feedback
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   Ubuntu 19.04 with Apache 2.4.38
 PHP Version:        7.2.22
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2019-09-12 20:18:36] 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:16:34] 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:15:52] 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:13:42] 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


------------------------------------------------------------------------



--
Edit this bug report at https://bugs.php.net/bug.php?id=78530&edit=1


Thread (6 messages)

« previous php.bugs (#222716) next »