Req #32529 [Opn->Wfx]: bsprintf?

From: Date: Wed, 31 Dec 2014 16:12:04 +0000
Subject: Req #32529 [Opn->Wfx]: bsprintf?
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-189563@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=32529&edit=1 ID: 32529 Updated by: nikic@php.net Reported by: andala at gmail dot com Summary: bsprintf? -Status: Open +Status: Wont fix Type: Feature/Change Request -Package: Feature/Change Request +Package: *General Issues Operating System: MacOS X PHP Version: 5CVS-2005-04-01 (dev) Block user comment: N Private report: N New Comment: This function is too specific for inclusion in the standard library. Previous Comments: ------------------------------------------------------------------------ [2005-04-01 07:35:19] andala at gmail dot com 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! ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=32529&edit=1

« previous php.bugs (#189563) next »