Quick Table of Contents
Outputting Data to the Browser
Of course, even the simplest of dynamic web sites will output data to the browser, and PHP offers several methods for doing so. [Edit] The print() Statement
The print() statement outputs data passed to it . Its prototype looks like this: int print(argument)All of the following are plausible print() statements: <?php print("<p>Hello World</p>"); ?> <?php $season = "summertime"; print "<p>I love the $season.</p>"; ?>All these statements produce identical output: I love the summertime.Note Although the official syntax calls for the use of parentheses to enclose the argument, they’re not required because print() isn’t technically a function; it’s a language construct. Many programmers tend to forgo them simply because the target argument is equally apparent without them. The print() statement’s return value is misleading because it will always return 1 regardless of outcome (the only outcome I’ve ever experienced using this statement is one in which the desired output is sent to the browser). This differs from the behavior of most other functions in the sense that their return value often serves as an indicator of whether the function executed as intended. [Edit] The echo() Statement
Alternatively, you could use the echo() statement for the same purposes as print(). While there are technical differences between echo() and print(), they’ll be irrelevant to most readers and therefore aren’t discussed here. echo()’s prototype looks like this: void echo(string argument1 [, ...string argumentN])To use echo(), just provide it with an argument just as was done with print(): echo "I love the summertime.";As you can see from the prototype, echo() is capable of outputting multiple strings. The utility of this particular trait is questionable; using it seems to be a matter of preference more than anything else. Nonetheless, it’s available should you feel the need. Here’s an example: <?php $he = "Brad"; $she = "Angelina"; echo $he, " and ", $she, " are great couple."; ?>This code produces the following: Brad and Angelina are great couple.Executing the following (in my mind, more concise) variation of the above syntax produces the same output: echo "$he and $she are great couple.";If you hail from a programming background using the C language, you might prefer using the printf() statement, introduced next, when outputting a blend of static text and dynamic information. Tip Which is faster, echo() or print()? The fact that they are functionally interchangeable leaves many pondering this question. The answer is that the echo() function is a tad faster because it returns nothing, whereas print() will return 1 if the statement is successfully output. It’s rather unlikely that you’ll notice any speed difference, however, so you can consider the usage decision to be one of stylistic concern. [Edit] The printf() Statement
The printf() statement is ideal when you want to output a blend of static text and dynamic information stored within one or several variables. It’s ideal for two reasons. First, it neatly separates the static and dynamic data into two distinct sections, allowing for easy maintenance. Second, printf() allows you to wield considerable control over how the dynamic information is rendered to the screen in terms of its type, precision, alignment, and position. Its prototype looks like this: integer printf(string format [, mixed args])For example, suppose you wanted to insert a single dynamic integer value into an otherwise static string: printf("Please Wait %d seconds", 30);Executing this command produces the following: Please Wait 30 secondsIn this example, %d is a placeholder known as a type specifier, and the d indicates an integer value will be placed in that position. When the printf() statement executes, the lone argument, 100, will be inserted into the placeholder. Remember that an integer is expected, so if you pass along a number including a decimal value (known as a float), it will be rounded down to the closest integer. If you pass along 30.2 or 30.6, then 30 will be output. Pass along a string value such as “one hundred”, and 0 will be output, although if you pass along 123food, then 123 will be output. Similar logic applies to other type specifiers (see this Table for a list of commonly used specifiers).
[Edit] The sprintf() Statement
The sprintf() statement is functionally identical to printf() except that the output is assigned to a string rather than rendered to the browser. The prototype follows: string sprintf(string format [, mixed arguments])An example follows: $cost = sprintf("$%.2f", 43.2); // $cost = $43.20 |
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
© Advanced Web Core. All rights reserved