PHP/Utility Function/print
Содержание
- 1 Code Block Containing Multiple print() Statements
- 2 Interpolating array element values in double-quoted strings
- 3 Interpolating array element values with curly braces
- 4 Mix the output of date( ) with a text string to get a natural-looking statement
- 5 Mix variable with string output
- 6 Output string value
- 7 Output value of a variable wit print function
- 8 Printing a form
- 9 Using parentheses with these constructs
Code Block Containing Multiple print() Statements
<html>
<head><title>A Code Block Containing Multiple print() statements</title></head>
<body>
<div>
<?php
$display_prices = true;
if ( $display_prices ) {
print "<table>";
print "<tr><td colspan=\"3\">";
print "todays prices in dollars";
print "</td></tr><tr>";
print "<td>14</td><td>32</td><td>71</td>";
print "</tr></table>";
}
?>
</div>
</body>
</html>
Interpolating array element values in double-quoted strings
<?
$meals["breakfast"] = "A";
$meals["lunch"] = "B";
$amounts = array(3, 6);
print "$meals[breakfast]";
print "$meals[lunch]. $amounts[0]";
print "$amounts[1].";
Interpolating array element values with curly braces
<?
$meals["A"] = "$3.95";
$hosts["www.example.ru"] = "web site";
print "{$meals["Walnut Bun"]}.";
print "{$hosts["www.example.ru"]}.";
?>
Mix the output of date( ) with a text string to get a natural-looking statement
print "The day yesterday was " . date("l", time( ) - 86400);
Mix variable with string output
<?php
$cars = 10;
print "We have $cars cars.\n<br>";
print "We got another new car.\n<br>";
$cars++;
print "Now we have $cars cars!\n<p>";
print "<b>$cars++</b> is the same to PHP as <b>$cars + 1.</b>";
?>
Output string value
$first_name = "James";
$last_name = "Mc";
$full_name = "$first_name $last_name";
print $full_name;
print strlen($full_name);
Output value of a variable wit print function
$total = 123.123;
print "Total cost of the meal is \$$total";
Printing a form
<?php
print <<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Name: <input type="text" name="user">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
?>
Using parentheses with these constructs
<?php
print("Hello!");
?>