Sending Text to the Browser PHP

Any text outside PHP tags is automatically sent to the browser. This is as you would Expect PHP offers three functions that simply send text to the browser: echo , print , and printf .

The echo string first, string second, . . ., string last

The echo function sends any number of parameters, separated by commas, to the browser. Each will be converted to a string and printed with no space between them. Unlike most other PHP functions, the echo function does not require parentheses. In fact, echo is more of a statement than a function.

echo "First string", 2, 3.4, "last string";


As text is sent to the browser via functions like print and echo, it may be stored in a memory buffer and written out only when the buffer fills. The flush function attempts to force the buffer to be dumped to the browser immediately. Since the Web server ultimately controls communication with the browser, the flush may not be effective. If your script takes a long time to execute, it's a good idea to output a status message and flush the buffer. This keeps the user from clicking away.

//simulate long calculation
//flush output buffer with each step
for($n=0; $n5; $n++)

print(string output)

The output argument of print is sent to the browser.

print("hello world!BR> ");

printf(string format, . . .)

The printf function converts and outputs arguments to the browser based on a format string. The ormat string contains codes, listed in Table 8. 1, for different data types. These codes begin ith a percentage sign, %, and end with a letter that determines the type of data. The codes match up with a list of values that follow the format string in the rgument list. Any text outside these codes will be sent unchanged to the browser.

You also have the option of placing characters between the % and the type specifier that control how the data is formatted. Immediately following the % you may place any number of flags. These flags control padding and alignment.

After any flags, you may specify a minimum field length. The converted output will be printed in a field at least this wide, longer if necessary. If the output is shorter than the minimum width, it will be padded with a padding character, a space by default. The padding will normally be placed to the left but, if the - flag is used, it will be placed to the right.

Next, you may specify a precision. It must start with a period to separate it from the minimum field length. For strings, the precision is taken to mean a maximum field length. For doubles, the precision is the number of digits that appear after the decimal point. Precision has no meaning for integers.


printf("%-10s %5d %05.5f <BR> ", "a string", 10, 3.14);

Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd Protection Status

PHP Topics