Display and Format Strings in PHP

PHP provides several functions that are useful for displaying and formatting strings. First we will discuss PHP's two basic language constructs for outputting strings: echo and print. We will describe and demonstrate the various formatting functions below, including wordwrap, nl2br, number_format. We also discuss why we often use var_dump to display strings.

echo and print

To display strings, you will commonly use echo or print. Since they are language constructs and not functions, there is no need to enclose the data passed to them in parentheses, although you can. Here we demonstrate both:

$val = 'A string to display';
// echo and print without parentheses
echo $val;
print $val;

// echo and print with parentheses
echo($val);
print($val);

// result is same for all the above:
// A string to display

Although echo and print are very similar, there are some differences. While echo has no return value, print returns 1; while echo can accept multiple arguments (if they are not enclosed in parentheses), print can only accept one. You can use the concatenation operator to pass multiple values to either echo or print:

$first = 'Jane';
$last = 'Doe';
// using concatenation operator to build single argument
echo 'Hello, my name is ' . $first . ' ' . $last . '. What\'s yours?';
print 'Hello, my name is ' . $first . ' ' . $last . '. What\'s yours?';
// result for each:
// Hello, my name is Jane Doe. What's yours?

Controlling or Formatting Display

PHP provides several functions for formatting strings or controlling how they display. We discuss and demonstrate them here.

wordwrap

The wordwrap function controls line wrapping in the string you pass to it. An optional second argument allows you to specify width (number of characters); otherwise a default width of 75 will be applied. The following example demonstrates, showing the result in both the browser window and page source view :

$str = 'Here is a relatively long string to demonstrate how the wordwrap function operates.';
// pass string and (optional) width to wordwrap
echo wordwrap($val, 50);
/* 
// browser window display:
Here is a relatively long string to demonstrate how the wordwrap function operates.

// View Source display:
Here is a relatively long string to demonstrate
how the wordwrap function operates.
*/

Notice that the function has had no effect on how the string displays in the browser, whereas you do see the lines wrapped when you view the page source. This is because the default line break character for wordwrap is \n, which has no effect in the browser (hence the need for the nl2br function described below).

An optional third argument to the wordwrap function allows you to specify line break character(s). We demonstrate passing "<br />\n" as the third argument in the following example:

// pass string, width, and line break characters
echo wordwrap($str, 50, "<br />\n");
/*
// browser window display:
Here is a relatively long string to demonstrate
how the wordwrap function operates. 

// View Source display:
Here is a relatively long string to demonstrate<br />
how the wordwrap function operates.
*/

Now the line breaks appear in the browser window too. When we view the page source, we see the <br /> tags in the markup.

An optional fourth argument to the wordwrap function allows you to specify whether a long word will be cut in the middle according to your width specification. The default value for the fourth argument is false; pass true to introduce line breaks in the middle of words.

nl2br

Perhaps you have noticed that if you insert new lines into your strings, those new lines are ignored when the content is displayed in a browser. However, if you view page source, you see the new lines there.

The <br /> tag is the HTML equivalent of the new line (\n). The nl2br function provides the means of converting new lines in your strings to the <br /> tag. The following example demonstrates:

$str = "Here is a relatively long sentence 
    with a few line breaks to demonstrate \nhow the nl2br function works.";
// pass $str to nl2br
echo nl2br($str);
/* 
// browser window display:
Here is a relatively long sentence
with a few line breaks to demonstrate
how the nl2br function works. 

// View Source display:
Here is a relatively long sentence <br />
    with a few line breaks to demonstrate <br />
how the nl2br function works.
*/

Notice that whether the new lines are created using carriage returns or the new line escape sequence (\n), the nl2br function converts them to <br /> tags.

number_format

The number_format function does just what its name implies: it formats numbers. If you pass a floating point number with no additional arguments, a comma will separate thousands, no decimal places will be included, and the number will be rounded, as the following demonstrates:

echo number_format(2546825.85); // 2,546,826

An optional second argument to number_format specifies the number of decimal places to display:

$num = 3 + 1.60;
// display $num with echo alone
echo $num; // 4.6
// number_format specifying 2 decimal places
echo number_format($num, 2); // 4.60

Optional third and fourth arguments to the number_format function allow you to specify a decimal point character and thousands separator for other languages.

Using var_dump for Information with Output

Throughout this tutorial we often use the var_dump function to display results since it provides more information about the data it displays. It includes the data type, length, and value of the variable passed to it. Plus it encloses strings in quotes, which is very helpful for showing exact results. Consider the following simple example:

// string with extra spaces
$str = "    Hello World! ";
echo $str; // Hello World! (browser display)
// display using var_dump
var_dump($str);  
// string(17) " Hello World! " (browser display)
// string(17) "    Hello World! " (View Source display)
// trim, then display result using var_dump
var_dump( trim($str) ); // string(12) "Hello World!" 

When we display the example string above using echo, we can't see the extra spaces. If we apply trim and display the result using echo, we can't see whether we have succeeded in trimming the string. When we use var_dump we can see the data more clearly. However, notice that although it says the string length is 17 characters, we don't see all 17 characters displayed between the quotes in the browser window. You can either View Source, or display the output of var_dump inside <pre> tags to get an accurate view:

// enclose var_dump output in pre tags
echo '<pre>';
var_dump($str);
echo '</pre>';
// string(17) "    Hello World! " (browser display)

While the browser generally ignores extra spaces and carriage returns, they are accurately displayed inside <pre> tags. This is especially helpful for displaying arrays with var_dump:

$ar = [0, 'one', 2, true];
var_dump($ar);
// browser display:
// array(4) { [0]=> int(0) [1]=> string(3) "one" [2]=> int(2) [3]=> bool(true) } 

echo '<pre>';
var_dump($ar);
echo '</pre>';
/*
// browser display:
array(4) {
  [0]=>
  int(0)
  [1]=>
  string(3) "one"
  [2]=>
  int(2)
  [3]=>
  bool(true)
}
*/

Back to top