Searching...
Wednesday 12 June 2013

PHP Basics - Part 3 :: 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.

Note: PROTOTYPE- It is simply the function’s definition, formalizing its name, input parameters, and the type of value it returns, defined by a data type.
More about functions in php 

The print() Statement

The print() statement outputs data passed to it . Its prototype looks like this:

int print(argument)

The print() statement’s return value is misleading because it will always return 1 regardless of outcome.

All of the following are plausible print() statements:

<?php
print("<p>I love the summertime.</p>");
?>

<?php
$season = "summertime";
print "<p>I love the $season.</p>";
// it doen't matter if you don't use parentheses because technically they are not functions but language constructs
?>

<?php
print "<p>I love the
summertime.</p>";
?>

All these statements produce identical output:

I love the summertime.

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
$heavyweight = "Lennox Lewis";
$lightweight = "Floyd Mayweather";
echo $heavyweight, " and ", $lightweight, " are great fighters.";
?>

<?php
echo "$heavyweight and $lightweight are great fighters.";
?>

Both the codes produces the following:

Lennox Lewis and Floyd Mayweather are great fighters.

Note: 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.

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. (The ones from c background must be familiar of it and must prefer it over other methods of outputting data)

  • 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("Bar inventory: %d bottles of tonic water.", 100);

Executing this command produces the following:

Bar inventory: 100 bottles of tonic water.

In 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 100.2 or 100.6, then 100 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 Table for a list of commonly used specifiers).

Commonly Used Type Specifiers
Commonly Used Type Specifiers



Another Example to be familiar with:-

printf("%d bottles of tonic water cost $%f", 100, 43.20);

Executing this command produces the following:

100 bottles of tonic water cost $43.200000

Because this isn't the ideal monetary representation, when working with decimal values, you can adjust the precision using a precision specifier. An example follows:

printf("$%.2f", 43.2); // $43.20

Still other specifiers exist for tweaking the argument’s alignment, padding, sign, and width. Follow this link:

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


1 comments:

 
Back to top!