Default Syntax
The default delimiter syntax opens with < ? php and concludes with ? >, like this:
Result:
Short-Tags
For less motivated typists, an even shorter delimiter syntax is available. Known as short-tags, this syntax forgoes the php reference required in the default syntax. However, to use this feature, we need to enable PHP’s short_open_tag directive. An example follows:
Important: Although short-tag delimiters are convenient, do not use them when creating PHP-driven software intended for redistribution. This is because this feature could potentially be disabled within the php.ini file.
When short-tags syntax is enabled and you want to quickly escape to and from PHP to output a bit of dynamic text, you can omit these statements using an output variation known as short-circuit syntax:
This is functionally equivalent to both of the following variations:
Embedding Multiple Code Blocks
We can escape to and from PHP as many times as required within a given page. For instance, the following example is perfectly acceptable:
As we can see here, any variables declared in a prior code block are remembered for later blocks, as is the case with the $date variable in this example.
Outputting Data to the Browser
There are many several methods to output the data on our browsers.
The print() Statement
All of the following are plausible print() statements:
All these statements produce identical output:
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.
The echo() Statement
Alternatively, we could use the echo() statement for the same purposes as print().
To use echo(), just provide it with an argument just as was done with print():
echo "I love Ho Chi Minh city.";
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:
or executing followings:
All above code will produce the following:
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 we’ll notice any speed difference, however, so we can consider the usage decision to be one of stylistic concern.
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.
- It neatly separates the static and dynamic data into two distinct sections, allowing for easy maintenance.
- printf() allows us to wield considerable control over how the dynamic information is rendered to the screen in terms of its type, precision, alignment, and position.
The output will be
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.
Note that an integer is expected, so if we pass along a number including a decimal value (known as a float), it will be rounded down to the closest integer. For example: if we pass
along 100.2 or 100.6, then 100 will be output or pass along a string value such as “one hundred”, and 0 will be output, although if we pass along 123food, then 123 will be output.
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:
Output: