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:
The echo function
The echo function sends a string (enclosed in quotation marks) to the browsers. The syntax of this function is as follows:
The expression can be a string or an expression that will be evaluated by the interpreter:
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:
Where expression defines a string or an expression that will be evaluated by the interpreter:
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:
|%b||integer in binary notation|
|%c|| character encoded using its ASCII code|
|%d||integer in decimal notation|
|%e||Double type (float number) in scientific format (1.76e +3)|
|%f||Double type (float number) |
|%o||Integer in octal|
|%x||Integer in hexadecimal notation (lowercase letters)|
|%X||Integer in hexadecimal notation (capital letters)|
Original document published on CommentcaMarche.net
Published by deri58
Latest update on June 25, 2013 at 10:27 AM by deri58.