User Contributed Notes: printf
maird@webfeat.ca
22-Sep-1999 12:43
Basic Example:
printf("QueryString is: %s this time",$StringName);
Anonymous Coward (albeit helpful)
01-Oct-1999 06:50
to get a <pre>% in printf use %%</pre> (as in the C-version,
I believe to remember)
pweinberg@mail.plymouth.edu
12-Oct-1999 10:19
echo vs. print vs. printf
echo simply spits out what you want. print() is a function that will return either 0 or 1 based on failure or success. printf is similar but is used for printing formatted output.
scott@mha.ca
17-Nov-1999 09:26
<B>warning:</B>
the following command may not return what you expect:
<PRE>
printf ("(9.95 * 100) = %d \n", (9.95 * 100));
</PRE>
i am using php 3.0.11 and this command will print a result of 994. This also happens if you try to do an intval() first, so this looks like it is something to be wary of if you use float values at all and convert them to integers at any point. -- The one way that i have found around this is to add 0.5 before doing an intval() which seems to counteract whatever is happening under-the-hood to cause this problem, for example:
<PRE>
printf ("(9.95 * 100) = %d \n", intval((9.95 * 100) + 0.5));
</PRE>
this seems to work fine, but it is kind of alarming that it does not work otherwise -- which leads me to be skeptical of even this fix working every time because i am not sure why this would be necessary.
dhosek@excite.com
05-Dec-1999 02:27
Scott, the problem that you're encountering is because of two things:
First %d converts a float to an int by truncation.
Second floats are notorious for tiny little rounding errors. If you rerun your program with %f in place of the %d I'll bet money that you'll get 94.99999999999999 or somesuch
jarred@coltonsolutions.com
23-Feb-2000 05:03
As far as I know, you should be able to simply include in the format string, so something like this:
printf("Value of I: %d\n", $i);
printf is the C/C++ style of doing formatted output, and as such the only special characters in the format string are '\' and '%', and characters immediately following these special cases may be affected. (So "%c" mean a character, while "%%c" means print a percent sign followed by the letter c.)
prasetyas@usa.net
23-Mar-2000 01:54
<PRE>print()</PRE> is will not return 0 or 1. It will just display the variable you put between the brackets.
Example:
<PRE>
$row = mysql_fetch_array($query_result);
print($row["TITLE"]);
</PRE>
It will display the content of the field "TITLE", which is a part of the array "$row".
jeremiah@alcyon.net
06-Jul-2000 10:15
Someone mentioned that the dropping of digits is pernicious; here's a small function you can throw in an include file or something to ensure some accurate precision.
//---------------------
// float precision (float, integer)
//
// - jlph 6.30.2000
//
// A function to limit the # of decimal places
// displayed in a float. Takes a float and an integer.
// The float will be taken to the decimal place
// specified by the integer and rounded up accordingly.
// Returns a float with the corners smoothed off.
//---------------------
function precision ($value, $place){
$value = $value * pow(10, $place + 1);
$value = floor($value);
$value = (float) $value/10;
(float) $modSquad = ($value - floor($value));
$value = floor($value);
if ($modSquad > .5){
$value++;
}
return $value / (pow(10, $place));
}
//---------------------
wtuomela@calpoly.edu
14-Jul-2000 02:57
Instead of using the precision function above, you can just use printf's precision for floats. It's just like C, "%.(precision)f". ex. printf("%.2f", $float); Will print the variable $float with 2 decimal places.