w3resource

PHP String Exercises : Format values in currency style

PHP String: Exercise-8 with Solution

Write a PHP script to format values in currency style.

Sample values : value1 = 65.45, value2 = 104.35

Sample Solution:

PHP Code:

<?php
$value1 = 65.45;
$value2 = 104.35;
echo sprintf("%1.2f", $value1+$value2)."\n";
?>

Sample Output:

169.80

Flowchart :

Flowchart: Format values in currency style

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to get the last three characters of a string.
Next: Write a PHP script to generate simple random password [do not use rand() function] from a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

PHP: Tips of the Day

$_COOKIE: This SuperGlobal Variable is used to retrieve Cookie value with given Key.

Example:

<?php
$cookie_name = "data";
$cookie_value = "Foo Bar";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); 
if(!isset($_COOKIE[$cookie_name])) {
 echo "Cookie named '" . $cookie_name . "' is not set!";
}
else {
 echo "Cookie '" . $cookie_name . "' is set!";
 echo "\n";
 echo "Value is: " . $_COOKIE[$cookie_value];
}
?>

Output:

Cookie named 'data' is not set!