[PHP-BUG] Bug #70618 [NEW]: Ternary eccentricity

[email protected] ("symphonymous at hotmail dot com")
Newsgroups php.standards
Message-ID <[email protected]>
From:             symphonymous at hotmail dot com
Operating system: Ubuntu
PHP version:      5.6.14RC1
Package:          PHP Language Specification
Bug Type:         Bug
Bug description:Ternary eccentricity

Description:
------------
I suspect this is a bug as the behaviour is a little worrying.

I was using the following in a Laravel program

    $fieldLabel = // has a field label been manually defined
      ($fieldLabel =  $this->hasAttribute($fieldValue, 'FieldLabel'))
      ? $fieldLabel
      : ($this->parseFieldType($fieldValue) != 'hidden')         
        ? ucfirst($fieldKey)
        : '';

However when $label was defined it was resolving to 
$fieldLabel = ucfirst($fieldKey); 

(ie. the second ternary).

the resolution was to parenthesise the second ternary (which I should
have done in the first place):

    $fieldLabel = // has a field label been manually defined
      ($fieldLabel =  $this->hasAttribute($fieldValue, 'FieldLabel'))
      ? $fieldLabel
      : (($this->parseFieldType($fieldValue) != 'hidden')         
        ? ucfirst($fieldKey)
        : '');

The bug, in my eyes, is that aforementioned code should have thrown an
exception as the ternary statement gave an ambiguous result.

best

Jon

Test script:
---------------
// here is the same issue again in a simpler form, tested with 
// "php artisan tinker"

$foo = 1;

$bar = ($bar = $foo) ? $bar : ($bar = 2) ? 2 : 'null';

Expected result:
----------------
either an exception as the statement is ambiguous or 1;

I would suggest the former.

Actual result:
--------------
2

-- 
Edit bug report at https://bugs.php.net/bug.php?id=70618&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=70618&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=70618&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=70618&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=70618&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=70618&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=70618&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=70618&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=70618&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=70618&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=70618&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=70618&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=70618&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=70618&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70618&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=70618&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=70618&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=70618&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70618&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=70618&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=70618&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.