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 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:
Where expression defines a string or an expression that will be evaluated by the interpreter:
The syntax of printf () is as follows:
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:
The three standard functions
PHP provides three functions to send text to the browser:- echo
- 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:
<HTML> <HEAD> <TITLE>Display the date</TITLE> </HEAD> <BODY> <? // Retrieve date // store in a variable $MyDate = date("Y"); echo "We are on the $MyDate"; ?> </BODY> </HTML>
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:
Code | Format type |
---|---|
%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 |
%s | String |
%x | Integer in hexadecimal notation (lowercase letters) |
%X | Integer in hexadecimal notation (capital letters) |
%% | % Character |
0 comments:
Post a Comment