News Feed
Jobs Feed
Sections




News Archive
Court Ewing's Blog:
Forget Concatenation; Format your strings!
September 16, 2010 @ 08:38:35

On his blog today Court Ewing has posted a tutorial about a different approach to merging strings while formatting them at the same time - using sprintf and printf for more than just a single-shot output.

I do it, you do it, everyone does it! We all concatenate. If you're simply combining a few variables or constants together, concatenation is the way to go. After all, it is quick and easy, and who can complain about that? However, concatenation does have two serious drawbacks: any sort of string formatting must be done manually, and it is difficult to visualize the "goal" string when it is sufficiently complex.

He talks about the benefits of string formatting over basic string concatination like how easy it makes casting variable values - multiple or single - without you having to cast them manually and append. He gives a few code examples of how it can be used for simple formatting and how it can make escaping data used in multiple spots easier. He also includes a SQL query example showing the difference between using sprintf and a normal concatinated statement.

0 comments voice your opinion now!
format string concatination sprintf printf tutorial


blog comments powered by Disqus

Similar Posts

Builder.com.au: Text-size switching with PHP and CSS

James Morris' Blog: Deploy a Silex App Using Git Push

Zend Developer Zone: Indexing Email Messages with PHP, Zend Lucene and Sphinx

O'Reilly: Migrating Web-Based PHP Applications to Ajax

PHPMaster.com: REST - Can You do More than Spell It? Part 3


Community Events











Don't see your event here?
Let us know!


functional code framework example series phpunit development language zendframework2 podcast release application community api unittest interview introduction testing composer opinion

All content copyright, 2013 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework