|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2014-12-31 16:12 UTC] nikic@php.net
-Status: Open
+Status: Wont fix
-Package: Feature/Change Request
+Package: *General Issues
[2014-12-31 16:12 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 13 21:00:01 2025 UTC |
Description: ------------ Not sure how open you are to feature requests like this. I use my own function bsprintf() (i.e. boolean sprintf) quite a bit for display code. What it does is return $var outputted according to $format *only* if $var is not empty, otherwise return nothing. If (optional) $default is passed in, $default is returned instead of nothing if $var is empty. function bsprintf($var,$format='%s',$default="") { if (!empty($var)) {return sprintf($format,$var);} else if (!empty($default)) return $default; }; Thought I'd suggest implementing something like this as a built-in function. Here is a brief example of the type of use that I've found useful, mostly to keep code simple, condensed, and readable (especially with long scripts): ---- USING bsprintf ----- $apples=100; echo "% Apples: ".bsprintf($apples,'%s%%',"None.")." / % Oranges: ".bsprintf($oranges,'%s%%',"None."); (Total lines: 2) Returns: % Apples: 100% / % Oranges: None. ---- NOT USING bsprintf ----- $apples=100; echo "% Apples: "; echo (!empty($apples)) ? $apples."%" : "None."; echo " / % Oranges: "; echo (!empty($oranges)) ? $oranges."%" : "None."; (OR) $apples=100; echo "% Apples: ".((!empty($apples)) ? $apples."%" : "None.")." / % Oranges: ".((!empty($oranges)) ? $oranges."%" : "None."); (Total lines: 5, or 2-- including a really ugly, hard to read one) Returns: same as above!