Friday, January 7, 2011

php - working with function and sprintf()

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
 <title>Untitled</title>
</head>
<body>

<?php

// define function
function getCircumference($radius){
 // return value 

 return (2*pi()*$radius);
}


//
// ex 1.
//


print "The answer is ".sprintf("%4.2f",getCircumference(20));
echo '<br />';


//
// ex 2.
//


$num=getCircumference(20);
$txtt='.';

$txt=sprintf("The answer is %4.2f%s", $num, $txtt);
echo $txt;
echo '<br />';


//
// ex 3.
//


// this prints
printf("printf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';


// but, this does NOT print!!!
sprintf("sprintf: The answer is %4.2f%s", $num, $txtt);
echo '<br />';


?>
</body>
</html>

No comments:

Post a Comment