r/PHPhelp • u/grsftw • Jan 05 '22
Question: Why do PHP developers hate sprintf()?
I just can't figure it out. My background is in other languages. I get that sprintf() has had some fun issues in C when people don't properly account for length. But in PHP I see code all the time that, instead of using a very clear and concise sprintf(), use concatenations that are impossible to understand easily. Where does this come from? I'm used to something along the lines of:
$s = sprintf("My %s has %d %s and that makes me %s",
$spouse, $petNumber, $petType, ($goodEmotion ? "happy" : "sad"));
In PHP, I am constantly seeing this:
$s = "My ".$spouse." has $petNumber $petType and that makes me "
.$goodEmotion ? "happy" : "sad";
I'm honestly curious where this comes from. Is there a history of issues with sprintf() in PHP or is there something else?
edit: updated in markdown to show code
12
Upvotes
1
u/kenlubin Jan 06 '22
I find named variables to be easier to read than positionally indexed variables in almost all cases. The major exception would be if the variables are interchangeable, like "WHERE foo IN (?, ?, ?, ?)".