We have been displaying information as we have progress through these tutorials, now we are going to take a step back and look in some detail about the different ways to display information from our php scripts
Echo Output string (s)
Syntax
echo (string str1 , [string str[]);
Echo can be used with “”", “‘”, and used the normal escape characters, like “\n” and \” to output newline and escaping characters respectively.
< ?php
$my_variable = "Whoopti La";
$your_variable " Ladi Dah";
echo "Hello World
$my_variable is on a new line,\n $your_variable is also on a new line, \n. \"I am in quotes\"."
echo "\n$my_variable is not $your_variable\n";
echo '$my_variable is not $your_variable';
echo "\nThanks very much for using echo, now onto printf.\"';
?>
Echo Results
Hello World
Whoopti La is on a new line,
Ladi Dah is also on a new line
“I am in quotes.”
Whoopti La is not Ladi Dah
$my_variable is not $your_variable
Thanks very much for using echo, now onto printf.
Printf
Those of you who have experience with C, will be at home with printf
d => Display argument as a decimal number
“%XNd” => display number D as decimal padded with N X(s)
b => Display an integer as a binary number (base 2 )
f => Display an integer as a floating-point number (double)
o=> Display an integer as an octal number (base 8 )
s => Display argument as a string
c => Display an integer as ASCII equivalent
x => Display an integer as a lowercase hexadecimal number (base 16)
X => Display an integer as an uppercase hexadecimal number (base 16)
%ns => Output Spaces, where n is an integer
Sample Code
< ?php
$my_number=650;
printf("Binary (Base 2): %b", $my_number );
printf("Octal (Base 8 ): %o", $my_number );
printf("Decimal (Base 10): %d", $my_number );
printf("Double: %f, $my_number );
printf("Padded Decimal: %07d", $my_number );
printf("Hex (Base 16) (lower) : %x", $my_number );
printf("Hex (Base 16) (upper): %X", $my_number );
printf("String: %s", $my_number );
printf("%50s\n", " Right Aligned");
printf("%-50s\n", "Left Aligned");
?>
Printf Output
Binary (Base 2): 1010001010
Octal (Base 8 ): 1212
Decimal (Base 10): 650
Double: 650.000000
Padded Decimal: 0000650
Hex (Base 16) (lower) : 28a
Hex (Base 16) (upper): 28A
String: 650
Right Aligned
Left Aligned
Using a Formatted String
printf() prints directly to the browswer, however in some cases, you might need to have the values stored so that we can use them within your program. Luckily you can use sprintf to format strings just like prinft() that can be used later .
$hexvalue = sprintf(”%X”, 100);
print “100 is $hexvalue in Hexadecimal”;


