[thelist] sprintf (WAS: MySQL/PHP Associative Array Insert)

Mike Migurski mike at saturn5.com
Mon Feb 3 13:07:02 CST 2003


>I've been doing PHP for a number of years and many people seem to really
>like using sprintf(), but I can't see why. What are the advantages of
>using printf() and sprintf() over just plain old echo or print?

Mainly cleanliness - in general, I try not to use it for short echo/print
statements, since it has a bit of visual overhead, but when I'm rolling
more than three or four variables into a string, and especially when that
string has some internal structure (it's a SQL statement, or a block of
markup), then I use it to help with visual clutter.

Recent example:

echo sprintf('<text_asset name="%s"
                          date_entered="%s"
                          deprecated="N"
                          modified="N"
                          author="1"
                          slide="%s"
                          kind="%s"
                          placed="%s"
                          pos_x="%s"
                          pos_y="%s"
                          content="%s">',
              get_name_from_key($node['primary_key']),
              get_creation_from_key($node['primary_key']),
              end($call_stack),
              $node['type'],
              $node['placed'],
              $node['pos_x'],
              $node['pos_y'],
              $node['content']);

---------------------------------------------------------------------
michal migurski- contact info and pgp key:
                 http://www.saturn5.com/mike/contact.html

                "Freedom! Horrible, horrible freedom!"






More information about the thelist mailing list