Bug #78530 [PATCH]: ternary operator not working properly
| From: | sinigagliaalberto@gmail.com | Date: | Thu, 12 Sep 2019 20:15:52 +0000 |
| Subject: | Bug #78530 [PATCH]: ternary operator not working properly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-222713@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
Patch added by: sinigagliaalberto@gmail.com
Reported by: sinigagliaalberto at gmail dot com
Summary: ternary operator not working properly
Status: Open
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:
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
Previous Comments:
------------------------------------------------------------------------
[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