php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79074 Add null colleasing and ternary operator for php.ini directive
Submitted: 2020-01-07 13:20 UTC Modified: 2020-02-13 09:31 UTC
Votes:1
Avg. Score:1.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: michael dot vorisek at email dot cz Assigned:
Status: Open Package: *General Issues
PHP Version: 7.4.1 OS: Any
Private report: No CVE-ID: None
 [2020-01-07 13:20 UTC] michael dot vorisek at email dot cz
Description:
------------
php.ini configuration is currently quite flexible, it allows variables, binary bitwise operations etc.

But currently there is no way to write conditional expressions. This is feature request for:
- null colleasing operator (very usefull for empty enviroment variables to assign some default value instead)
- ternary operator (for general conditions)


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-07 13:22 UTC] michael dot vorisek at email dot cz
Should be easy to implement like the binary operators: https://github.com/php/php-src/blob/php-7.4.1/Zend/zend_ini_parser.y

Please add to PHP 7.4 if possible.
 [2020-01-10 19:06 UTC] geekcom@php.net
Hi michael, bellow text is a snippet of php.ini packaged with PHP.

"If PHP can't find an expected directive because it is not set or is mistyped, a default value will be used."

Please see at:
https://github.com/php/php-src/blob/master/php.ini-production
 [2020-01-11 22:36 UTC] michael dot vorisek at email dot cz
> https://github.com/php/php-src/blob/master/php.ini-production#L40#L41

@geekcom@php.net Thanks, so I could write:
memory_limit = 32M
memory_limit = "${PHP_MEMORY_LIMIT}"

Is there any test for this behaviour in the PHP source?


> ternary operator

What do you think about native ternary operator for php.ini?
 [2020-02-10 00:54 UTC] video dot ice dot power at seznam dot cz
memory_limit = 32M
memory_limit = "${PHP_MEMORY_LIMIT}"

I have tested this and currently this config does not ignore the 2nd line if the variable is not set, i.e. it does not use "last default/correct value" but instead it uses "PHP default value"

Ternary operator is definitely needed for these cases or is there currently any solution without using shell before PHP is launched?
 [2020-02-10 01:04 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2020-02-10 01:04 UTC] requinix@php.net
> If PHP can't find an expected directive because it is not set or is mistyped, a default value will be used.
If you pull this sentence out of context it's easy to misinterpret what it means.

> ; Directives are specified using the following syntax:
> ; directive = value
> ; Directive names are *case sensitive* - foo=bar is different from FOO=bar.
> ; Directives are variables used to configure PHP or PHP extensions.
> ; There is no name validation.  If PHP can't find an expected
> ; directive because it is not set or is mistyped, a default value will be used.

The sentence actually means that if you typo the *name* of the directive, PHP will not complain and a default value will be used instead. So it's irrelevant as far as this request goes.

What is the real-world use case for a null coalesce or ternary? Why does *branching logic* need to be performed in a configuration file?
 [2020-02-10 01:45 UTC] michael dot vorisek at email dot cz
> The sentence actually means that if you typo the *name* of the directive, PHP will not complain and a default value will be used instead.
Yes, but it does not actually use default value it uses empty string - it should be documented this way also.

My specific use case is to set some PHP config value thru env. vars for **recursive** PHP execution, i.e. to be able to pass some configuration values to php subprocess started by exec('php ...') of which I do not have any control and this seems to me like the only way without extra shell script.

I propose adding ternary "a ? b ? c" and null colleasing operators "a ?? b". Both operators should evaluate if the "a" expression is empty (i.e. zero length or "0")

Then fallback will be easy to implement like:
memory_limit = ${PHP_MEMORY_LIMIT} ?? "32MB"
 [2020-02-13 09:31 UTC] cmb@php.net
-Status: Feedback +Status: Open
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 12:01:31 2024 UTC