PHP/Utility Function/print

Материал из Web эксперт
Перейти к: навигация, поиск

Code Block Containing Multiple print() Statements

   <source lang="html4strict">

<html> <head><title>A Code Block Containing Multiple print() statements</title></head> <body>

<?php $display_prices = true; if ( $display_prices ) {

print ""; print ""; print ""; print "
";
 print "todays prices in dollars";
print "
143271
";

} ?>

</body> </html>

 </source>
   
  


Interpolating array element values in double-quoted strings

   <source lang="html4strict">

<? $meals["breakfast"] = "A"; $meals["lunch"] = "B"; $amounts = array(3, 6); print "$meals[breakfast]"; print "$meals[lunch]. $amounts[0]"; print "$amounts[1].";

 </source>
   
  


Interpolating array element values with curly braces

   <source lang="html4strict">

<? $meals["A"] = "$3.95"; $hosts["www.example.ru"] = "web site"; print "{$meals["Walnut Bun"]}."; print "{$hosts["www.example.ru"]}."; ?>

 </source>
   
  


Mix the output of date( ) with a text string to get a natural-looking statement

   <source lang="html4strict">

print "The day yesterday was " . date("l", time( ) - 86400);

 </source>
   
  


Mix variable with string output

   <source lang="html4strict">

<?php

   $cars = 10; 
   print "We have $cars cars.\n
"; print "We got another new car.\n
"; $cars++;
print "Now we have $cars cars!\n

"; print "$cars++ is the same to PHP as $cars + 1."; ?> </source>

Output string value

   <source lang="html4strict">

$first_name = "James"; $last_name = "Mc"; $full_name = "$first_name $last_name"; print $full_name; print strlen($full_name);

 </source>
   
  


Output value of a variable wit print function

   <source lang="html4strict">

$total = 123.123; print "Total cost of the meal is \$$total";

 </source>
   
  


Printing a form

   <source lang="html4strict">

<?php

   print <<<_HTML_

<form method="post" action="$_SERVER[PHP_SELF]"> Your Name: <input type="text" name="user">
<input type="submit" value="Say Hello"> </form> _HTML_; ?>

 </source>
   
  


Using parentheses with these constructs

   <source lang="html4strict">

<?php

           print("Hello!");
   ?>
 
</source>