#32529 [NEW]: bsprintf?
| From: | andala at gmail dot com | Date: | Fri, 01 Apr 2005 05:35:20 +0000 |
| Subject: | #32529 [NEW]: bsprintf? | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-76147@lists.php.net to get a copy of this message | ||
From: andala at gmail dot com
Operating system: MacOS X
PHP version: 5CVS-2005-04-01 (dev)
PHP Bug Type: Feature/Change Request
Bug description: bsprintf?
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 bug report at http://bugs.php.net/?id=32529&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=32529&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=32529&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=32529&r=trysnapshot51
Fixed in CVS: http://bugs.php.net/fix.php?id=32529&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=32529&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=32529&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=32529&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=32529&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=32529&r=support
Expected behavior: http://bugs.php.net/fix.php?id=32529&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=32529&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=32529&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=32529&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=32529&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=32529&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=32529&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=32529&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=32529&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=32529&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=32529&r=mysqlcfg