Jump to: navigation, search

Php sprintf

From w3cyberlearnings

Contents

PHP function sprintf

This function is to format string.

Syntax sprintf

List of format

  •  %% - Returns a percent sign
  •  %b - Binary number
  •  %c - The character according to the ASCII value
  •  %d - Signed decimal number
  •  %e - Scientific notation (e.g. 1.2e+2)
  •  %u - Unsigned decimal number
  •  %f - Floating-point number (local settings aware)
  •  %F - Floating-point number (not local settings aware)
  •  %o - Octal number
  •  %s - String
  •  %x - Hexadecimal number (lowercase letters)
  •  %X - Hexadecimal number (uppercase letters)

arg1:

  • Any parameters will be inserted at the percent (%) signs in the string_format.
sprintf(string_format,arg1, arg2, arg3,..)

Example 1

<?php

$salary = 8000;
$spend = 50;
$name = 'David';
$msg = "%s is going to spend %u %% of his %u salary for the trip to China";
$str = sprintf($msg, $name, $spend, $salary);
echo $str;
?>

Output

David is going to spend 50 % of his 8000 salary for the trip to China

Example 2

<?php

$stage1 = 20;
$stage2 = 50;
$price = 150;

$msg = "Used-Iphone 4S cost %u$, this is 
	   %.2f%% (%.2f $) cheaper in January, and also %.2f%% (%.2f $) cheaper in May";
$str = sprintf($msg, $price, $stage1, (150 * ($stage1 / 100)), $stage2, (150 * ($stage2 / 100)));
echo $str;
?>

Output

Used-Iphone 4S cost 150$, this is 20.00% (30.00 $) cheaper in January, and also 50.00% (75.00 $) cheaper in May

Example 3

<?php
$stunum = 5;
$location = 'Lab';

$msg = 'There are %d students in the %s';

$str = sprintf($msg, $stunum, $location);
echo $str;
?>

Output

There are 5 students in the lab

Example 4

<?php
$number = 362525200;

echo sprintf("%.3e", $number); 

Output

3.625e+8

Example 5

<?php

$n = 88786838;
$u = -88786838;
$c = 65; // ASCII 65 is 'A'
// notice the double %%, this prints a literal '%' character
echo sprintf("%%b = '%b'\n", $n). '<br/>'; // binary representation
echo sprintf("%%c = '%c'\n", $c). '<br/>'; // print the ascii character, same as chr() function
echo sprintf("%%d = '%d'\n", $n). '<br/>'; // standard integer representation
echo sprintf("%%e = '%e'\n", $n). '<br/>'; // scientific notation
echo sprintf("%%u = '%u'\n", $n). '<br/>'; // unsigned integer representation of a positive integer
echo sprintf("%%u = '%u'\n", $u). '<br/>'; // unsigned integer representation of a negative integer
echo sprintf("%%f = '%f'\n", $n). '<br/>'; // floating point representation
echo sprintf("%%o = '%o'\n", $n). '<br/>'; // octal representation
echo sprintf("%%s = '%s'\n", $n). '<br/>'; // string representation
echo sprintf("%%x = '%x'\n", $n). '<br/>'; // hexadecimal representation (lower-case)
echo sprintf("%%X = '%X'\n", $n). '<br/>'; // hexadecimal representation (upper-case)
echo sprintf("%%+d = '%+d'\n", $n). '<br/>'; // sign specifier on a positive integer
echo sprintf("%%+d = '%+d'\n", $u). '<br/>'; // sign specifier on a negative integer

?>

Output

%b = '101010010101100011110010110' 
%c = 'A' 
%d = '88786838' 
%e = '8.878684e+7' 
%u = '88786838' 
%u = '4206180458' 
%f = '88786838.000000' 
%o = '522543626' 
%s = '88786838' 
%x = '54ac796' 
%X = '54AC796' 
%+d = '+88786838' 
%+d = '-88786838' 

Related Functions


Navigation
Web
SQL
MISC
References