HTML5 Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 139 posts at DZone. You can read more from them at their website. View Full User Profile

Formatted Strings With PHP

03.22.2013
| 2889 views |
  • submit to reddit

When you are using PHP to putput a string on the page most of the time you will use the syntax echo, which will take the following string and display that string in HTML.

You can even concaternate multiple strings or variables together to be outputted by the echo syntax.

Here is an example of using echo to output multiple options in a select box.

<?php
        $options = array(
                        'option1' => 'title1',
                        'option2' => 'title2',
                        'option3' => 'title3',
                        'option4' => 'title4',
                        'option5' => 'title5'
                        );
        echo '
<select>';
        foreach($options as $key => $val)
        {
            echo '
<option value="'.$key.'">'.$val.'</option>
';
        }
        echo '</selected>';
?>

As you can see from the echo syntax that you can create a HTML element by using PHP. This code uses a concatenated string to insert the variables into the value attribute and a title inside the option tag. This is quite easy to read and is fine to use in normal development but what if you were using this method to display a more complicated element with more attributes such as an image tag.

This will create 5 image tags which will populate the a number of different attributes.

<?php
        $options = array(
                        'image1' => array('class' => 'style', 'alt' => 'Image 1', 'title' => 'Image 1', 'width' => '100', 'height' => '100', 'src' => 'http://www.example.com/image1.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 2', 'title' => 'Image 2', 'width' => '200', 'height' => '200', 'src' => 'http://www.example.com/image2.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 3', 'title' => 'Image 3', 'width' => '300', 'height' => '300', 'src' => 'http://www.example.com/image3.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 4', 'title' => 'Image 4', 'width' => '400', 'height' => '400', 'src' => 'http://www.example.com/image4.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 5', 'title' => 'Image 5', 'width' => '500', 'height' => '500', 'src' => 'http://www.example.com/image5.jpg'),
                        );
        foreach($options as $key => $val)
        {
            echo '<img class="'.$val["class"].'" alt="'.$val["alt"].'" title="'.$val["title"].'" width="'.$val["width"].'" height="'.$val["height"].'" src="'.$val["src"].'" />';
        }
    ?>

This time if you look at the echo syntax to output the image tags it's not very readable and can be hard to work out what we are joining together to create the image tag, especially with the different single and double quotes it becomes really hard to read.

The solution is to PHP built in functions sprintf() and printf() to create a formatted string to output in the HTML.

Both these functions do the same thing but the printf() will echo the string directly and the sprintf() function will return a formatted string.

This is how it will be used to display the image tag.

Printf() Function Displaying Image Tag

<?php
        $options = array(
                        'image1' => array('class' => 'style', 'alt' => 'Image 1', 'title' => 'Image 1', 'width' => '100', 'height' => '100', 'src' => 'http://www.example.com/image1.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 2', 'title' => 'Image 2', 'width' => '200', 'height' => '200', 'src' => 'http://www.example.com/image2.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 3', 'title' => 'Image 3', 'width' => '300', 'height' => '300', 'src' => 'http://www.example.com/image3.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 4', 'title' => 'Image 4', 'width' => '400', 'height' => '400', 'src' => 'http://www.example.com/image4.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 5', 'title' => 'Image 5', 'width' => '500', 'height' => '500', 'src' => 'http://www.example.com/image5.jpg'),
                        );
        foreach($options as $key => $val)
        {
            printf('<img class="%s" alt="%s" title="%s" width="%s" height="%s" src="%s" />',
                    $val["class"],
                    $val["alt"],
                    $val["title"],
                    $val["width"],
                    $val["height"],
                    $val["src"]);
        }
    ?>

Sprintf() Function Displaying Image Tag

<?php
        $options = array(
                        'image1' => array('class' => 'style', 'alt' => 'Image 1', 'title' => 'Image 1', 'width' => '100', 'height' => '100', 'src' => 'http://www.example.com/image1.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 2', 'title' => 'Image 2', 'width' => '200', 'height' => '200', 'src' => 'http://www.example.com/image2.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 3', 'title' => 'Image 3', 'width' => '300', 'height' => '300', 'src' => 'http://www.example.com/image3.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 4', 'title' => 'Image 4', 'width' => '400', 'height' => '400', 'src' => 'http://www.example.com/image4.jpg'),
                        'image1' => array('class' => 'style', 'alt' => 'Image 5', 'title' => 'Image 5', 'width' => '500', 'height' => '500', 'src' => 'http://www.example.com/image5.jpg'),
                        );
        foreach($options as $key => $val)
        {
            $image = sprintf('<img class="%s" alt="%s" title="%s" width="%s" height="%s" src="%s" />',
                    $val["class"],
                    $val["alt"],
                    $val["title"],
                    $val["width"],
                    $val["height"],
                    $val["src"]);
            echo $image;
        }
    ?>

As you can see using the printf() and sprintf() functions allows you to much more readable code for outputting formatted strings.



Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)