Bug #78530 [NEW]: ternary operator not working properly

From: Date: Thu, 12 Sep 2019 20:13:42 +0000
Subject: Bug #78530 [NEW]: ternary operator not working properly
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222712@lists.php.net to get a copy of this message
From:             sinigagliaalberto at gmail dot com
Operating system: Ubuntu 19.04 with Apache 2.4.38
PHP version:      7.2.22
Package:          Scripting Engine problem
Bug Type:         Bug
Bug description:ternary operator not working properly

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 bug report at https://bugs.php.net/bug.php?id=78530&edit=1
-- 
Fix committed:                    https://bugs.php.net/fix.php?id=78530&r=fixed
Fixed in release:                 https://bugs.php.net/fix.php?id=78530&r=alreadyfixed
Need backtrace:                   https://bugs.php.net/fix.php?id=78530&r=needtrace
Need Reproduce Script:            https://bugs.php.net/fix.php?id=78530&r=needscript
Try newer version:                https://bugs.php.net/fix.php?id=78530&r=oldversion
Not developer issue:              https://bugs.php.net/fix.php?id=78530&r=support
Expected behavior:                https://bugs.php.net/fix.php?id=78530&r=notwrong
Not enough info:                  https://bugs.php.net/fix.php?id=78530&r=notenoughinfo
Submitted twice:                  https://bugs.php.net/fix.php?id=78530&r=submittedtwice
register_globals:                 https://bugs.php.net/fix.php?id=78530&r=globals
PHP version support discontinued: https://bugs.php.net/fix.php?id=78530&r=phptooold
Daylight Savings:                 https://bugs.php.net/fix.php?id=78530&r=dst
IIS Stability:                    https://bugs.php.net/fix.php?id=78530&r=isapi
Install GNU Sed:                  https://bugs.php.net/fix.php?id=78530&r=gnused
Floating point limitations:       https://bugs.php.net/fix.php?id=78530&r=float
No Zend Extensions:               https://bugs.php.net/fix.php?id=78530&r=nozend
MySQL Configuration Error:        https://bugs.php.net/fix.php?id=78530&r=mysqlcfg


Thread (6 messages)

« previous php.bugs (#222712) next »