php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12735 Shorthand for X ? X : Y
Submitted: 2001-08-14 03:30 UTC Modified: 2002-01-28 21:09 UTC
From: david@php.net Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.6 OS:
Private report: No CVE-ID: None
 [2001-08-14 03:30 UTC] david@php.net
Providing default values for fields, I find myself using the following syntax a lot:

$a = $result->f('asd'); if (!$a) $a = 'nothing';

$a = ($result->f('asd') ? $result->f('asd') : 'nothing');

Generally:

$a = ($x ? $x : $y);

It would be handy to have a shorthand for this that would easy typing, legibility, and avoid duplicate evaluation of $x.

Proposed syntax (not fussy)

$x ?: $y

i.e. if 'value if true' is empty, use 'condition value'.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-28 21:09 UTC] yohgaki@php.net
Won't change....
You can use "or"/"and" also.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Dec 26 10:01:29 2024 UTC