image


Valora:  3/5
Inicio » PHP » PHP Tutorial » Statements




Output

So far we have used the "echo" statement to perform output to screen, this statement is rather limited since it will not allows us to format the output. In this page we will learn about the statement "printf" which gives us much more power.

Statement printf
<?php
   printf(string format, variable1, variable2...);
?>

The format string indicates how to represent the values that will be indicated later. The main advantage is that, besides formatting the output values, we can also insert text between them.

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<?php
   printf("The number two: %d %f %.2f",2,2,2);
?>

</body>
</html>

Execute View code

The format string may include a series of special characters that indicate how to format the variables included in the statement.

Element Type of variable
%s String of characters.
%d Number with no decimals.
%f Number with decimals.
%c ASCII Character.
lthough there are other types, these are the most important ones.

<!-- PHP Tutorial WebEstilo.com -->
<html>
<head>
   <title>PHP Example</title>
</head>
<body>
<?php
   $var="text";
   $num=3;
   printf("<b>%s</b> and <b>%d</b> <br>",$var,$num);
   
   printf("<TABLE BORDER=1 CELLPADDING=20>");
   for ($i=0;$i<10;$i++)
   {
      printf("<tr><td>%10.d</td></tr>",$i);
   }
   printf("</table>");
?>

</body>
</html>

Execute View code





WebEstilo.com - Introduzca su e-mail y conozca las novedades. No hacemos Spam.
Google
  Web WebEstilo.com   
 
Valid HTML 4.01!
Última modificación:24 de Diciembre de 2004. Spain - España.
© 1998-2004 por Joaquin Gracia. Todos los derechos reservados.