Printing to the Screen with PHP

PHP is not much use unless you can actually output data to the browser. There are several ways to accomplish this task and it requires the use of any one of four different statements. These statements are:

  • echo
  • print()
  • printf()
  • sprintf()

What one you use and when depends on the circumstance. We will look at each one individually.

The Echo Statement

The echo statement is probably used the most out all of the output statements. Although you can use the parentheses when using the echo statement it is not necessary. As a matter of fact, if you want to output more than one string to the browser you cannot use the parenthesis. This is because echo is not technically a function but a language construct. Its prototype looks like this:

For example, if you wanted to output the text “Welcome to my website” using echo you would do it like this:

You can also use it with parenthesis

When outputting a string you can use either double quotes or single quotes depending on the need for interpolation. Interpolation will be explained in a future PHP tutorial on PHP variables. In the meantime simply use double quotes with the echoed strings.

The Print Statement

The prototype for the echo statement looks like this:

The print statement is used like this:

Like echo, print can be used with parenthesis.

Print is very similar to the echo statement in several ways. First, like the echo state the parenthesis are not required. Second, print is not a function.

There are some differences between the two. The echo does not return anything and the print statement always returns a value of 1. This makes it usable in an expression. Another difference is print can only take one parameter whereas echo can take multiple. The last difference is that echo is slightly faster than print.

The printf() Function

The printf() function is what you would use if you needed to output a formatted string. It differs from print and echo in that it is a function. The printf() function is more complex than the first two on the list but is also the most useful. It allows you to print a line that has static text and variables mixed. This is good for two reasons. First, it separates the static text from the variable which makes it more organized and easier to maintain. Second, it gives you a lot of control over how a variables content is displayed in terms of alignment, position, type, and precision.

The prototype looks like this:

Take the following code:

This code outputs:

Do you see the problem? The variables $total and $items will more than likely be figured out mathematically so you do not know what the actual number will be. The total number of items is okay but what about the dollar amount? A dollar amount needs 2 places after the decimal because it is currency. Even though the result is technically okay it needs to be formatted for currency. This is where printf() shines. We can take this same code and make sure that it is formatted correctly no matter what the value is. We can write the code like this:

*NOTE: Since we have not covered variables yet I will be using the number 5 and 125.1 directly. If you are familiar with variables just know that you can use variables in place of the actual numbers.

This will produce:

You can see the dollar amount is padded out to two places after the decimal. That is because of the specifier “%.2f”

A format string is made up of zero or more directives: ordinary characters that are copied to the result, and conversion specifications, each of which results in fetching its own parameter.

sprintf diagram

Each conversion specification starts with a percentage sign (%), followed by one or more elements. Elements can be considered special instructions for the conversion. In the above example “.2” would be an element.

  1. An optional sign specifier that forces a sign (- or +) to be used on a number. By default, only the – sign is used on a number if it’s negative.
  2. An optional padding specifier that says what character will be used for padding the results to the right string size. This may be a space character or a 0 (zero character). The default is to pad with spaces. An alternate padding character can be specified by prefixing it with a single quote (‘).
    An example would be:

    Would output:

    The ‘3 indicated what to fill the padding space with and the number 5 indicates how many spaces to pad, total. The ‘3 could be easily be replaced with any character.
  3. An optional alignment specifier that says if the result should be left-justified or right-justified. The default is right-justified; a “-“character here will make it left-justified.From our example above, if we replaced %’35.d above with %-’35.d the justification would be left aligned and we would get this printed to the screen:

     
  4. An optional number, a width specifier that says how many characters (minimum) this conversion should result in.
  5. An optional precision specifier in the form of a period (.) followed by an optional decimal digit string that says how many decimal digits should be displayed for floating-point numbers. When using this specifier on a string, it acts as a cutoff point, setting a maximum character limit to the string. Additionally, the character to use when padding a number may optionally be specified between the period and the digit. This is what we used in the total dollar amount we used in our example.
  6. A type specifier that says what type the argument data should be treated as.

Possible types:

Type Specifier Description Number Output
b The argument is treated as an integer, and presented as a binary number. 123456789 11101011011110…
c The argument is treated as an integer, and presented as the character with that ASCII value. Because of the quotes, this is treated as a string. “50” 2
d The argument is treated as an integer, and presented as a (signed) decimal number. 123456789 123456789
e The argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of significant digits (one less). 123456789 1.234568e+8
E Like %e but uses uppercase letter (e.g. 1.2E+2). 123456789 1.234568E+8
f The argument is treated as a float, and presented as a floating-point number (locale aware). 123456789 123456789.000000
F The argument is treated as a float, and presented as a floating-point number (non-locale aware). Available since PHP 4.3.10 and PHP 5.0.3. 123456789 123456789.000000
g Shorter version of e and f. 123456789 1.23457e+8
G Shorter version of E and f. 123456789 1.23457E+8
o The argument is treated as an integer, and presented as an octal number. 123456789 726746425
s The argument is treated as and presented as a string. 123456789 123456789
u The argument is treated as an integer, and presented as an unsigned decimal number. 123456789 123456789
x The argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). 123456789 75bcd15
X The argument is treated as an integer and presented as a hexadecimal number with uppercase letters. 123456789 75BCD15

The sprintf() function

Whew! That is a lot of information. Are you ready for the sprintf() function description? Here is goes:

Sprintf() works identically to the printf() function!

Okay not exactly. The diffenrece is printf outputs the formatted string directly to the screen when called. The sprintf function only returns the formatted string. All this means is you can copy it to a variable and then have to output it to the screen some other way. For example:

This code:

does exactly the same thing as this code but we need to use echo (or print) to get it to the screen:

If you have any question, feel free to leave a comment below.

Related Posts