A few words of thanks would be greatly appreciated.

PHP - Display text in the browser

The goal of PHP is to enable the creation of dynamic web pages, and as such, its primary purpose is to send data to the browser.

The three standard functions

PHP provides three functions to send text to the browser:
  • echo
  • print
  • printf

The echo function

The echo function sends a string (enclosed in quotation marks) to the browsers. The syntax of this function is as follows:
echo Expression;

The expression can be a string or an expression that will be evaluated by the interpreter:
  • echo "strings";
  • echo (1+2)*87;

Since the string is delimited by double quotes, inserting double quotes in the string cause an error. This is why the double quotes, and any special characters must be preceded by a backslash. Here is a summary of the special characters that requires the use of a backslash: (comma, the $ character, antislash, carriage return, newline, tabs).
The $ character has a special role to the extent that the interpreter consider it as a variable, which means that when the $ character is encountered in a string, the interpreter retrieves the name of the variable that follows the $ character and replaces it with its value.
In the following example example, the current date is assigned to a variable called $ MyDate and is displayed in the browser:


<TITLE>Display the date</TITLE>



// Retrieve date
// store in a variable
$MyDate = date("Y");

echo "We are on the $MyDate";




The print function

The print function is similar to the echo function with the difference that the expression to be displayed is find between parentheses. The syntax of the print function is as follows:
  • print(expression);

Where expression defines a string or an expression that will be evaluated by the interpreter:
  • print ("String of characters");
  • print ((1+2)*87);

The Printf function

The printf()function (borrowed from the C language) is rarely used because its syntax is more cumbersome. However, unlike the previous functions, it enables data formatting, which means that one can choose the format in which a variable will be displayed on the screen.
The syntax of printf () is as follows:
printf (formatted string);

A formatted string is a string containing special codes to identify where a value will be inserted and its format, that is to say its representation. Each code must be met with associated value or variable, that can be found as a parameter at the end of the printf function. The values ??to be inserted into the formatted string are separated by commas:

CodeFormat type
%binteger in binary notation
%c character encoded using its ASCII code
%dinteger in decimal notation
%eDouble type (float number) in scientific format (1.76e +3)
%fDouble type (float number)
%oInteger in octal
%xInteger in hexadecimal notation (lowercase letters)
%XInteger in hexadecimal notation (capital letters)
%%% Character

Original document published on CommentcaMarche.net

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.


This document, titled « PHP - Display text in the browser », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).