The strength of the web has been the ability for practically anyone to provide information (accurate or not) to anyone who is interested in viewing the web pages that contain that information. With HTML, the static nature of the information being displayed limits the amount of true customization that can be provided; however, PHP provides truly dynamic content by creating custom content each time the web page is displayed. To accomplish this display of information, PHP has four functions that make this happen:
This statement is designed to provide user feedback; in other words, this function "prints" content to the HTML code which in turn will be displayed on the web page when called. The print() function can display both raw strings and variables.
<?php
print('<p>This is the standard use of the print function.</p>');
print '<p>However, the parentheses are not necessary.</p>';
$var = "print";
print "<p>Variables can also be included with the $var
function.</p>";
print '<p>Another way to add a variable to a ' . $var .
' function is to concatenate it to the strings</p>';
?>
As shown above, there are multiple ways to use the print function. In addition, breaking the function components across multiple lines is acceptable. The function will continue to read the components until it reaches the semi-colon delimiter (;) which tells the function to complete the process.
• echo -
The echo function is similar to the print function in that it can "print" content to the HTML code; however, it can not be used in more complex expressions because it returns a void versus the print function which will return a boolean value. Except for this difference, the echo function is no different than the print function. With that thought in mind, many programmers choose to just use the print function to prevent this limitation as they advance in their programming skills.
echo '<p>This is the standard use of the echo function.</p>';
$var = 'echo';
echo "<p>The $functionType function can also use embedded
variables.</p>";
echo '<p>However, when concatenating with the ', $functionType,
' function, it is best to use commas to concatenate the
strings</p>';
?>
• printf() -
The printf function performs the same function as the print command with the exception that there is greater control over the formatting of the "printed" text. The printf function works from the standard print approach for "printing" text to HTML:
<?php
printf('<p>This is the standard use of the printf function</p>');
?>
Of course, the above demonstration does not add any value to the standard print function. But the printf function does not stop there, the following demonstrations show how the format argument can be included to provide a level of formatting to the strings referenced in the printf function:
<?php
printf('<p>%d Euro = $%01.2f US Dollar</p>', 1, 1.3);
printf('<p>My %s paid $%01.2f for my $s</p>', 'Mother', 23.4,
"Dog");
printf('<p>However, my %3\$s paid $%2\$01.2f to get away from my
%1\$s</p>', 'Mother', 100.3, 'Dog');
printf('<p>%11s</p>', 'Long text can be limited to a certain
number of characters, in this case 11.');
?>
A complete list of supported type specifiers is located in Appendix D. In the case of the third printf function listed above, the location of the string argument can be chosen by placing the location (#$) with the format type. In the case of %3\$s, the third argument was selected and formatted as a string. The reason for the escape character (\) preceding the dollar sign ($) is to ensure that it was considered part of the formatting versus an actual dollar sign as used in the first printf function.
The sprintf function performs the same functions as the printf function except it stores the result of the function into a variable.
<?php
$orderTotal = sprintf('$%01.2f', 123.456);
print "The order total is $orderTotal";
?>
Comments
Post a Comment