php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #32529 bsprintf?
Submitted: 2005-04-01 07:35 UTC Modified: 2014-12-31 16:12 UTC
From: andala at gmail dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5CVS-2005-04-01 (dev) OS: MacOS X
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: andala at gmail dot com
New email:
PHP Version: OS:

 

 [2005-04-01 07:35 UTC] 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!


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [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
This function is too specific for inclusion in the standard library.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 17 01:01:33 2025 UTC