printf() function in PHP || PHP printf() function
×


printf() function in php

1983

PHP printf() function is used to output a formatted string. It is an inbuilt function of PHP.

Note: Also read fprint() and sprint() function for same reference.

Syntax:

printf($format,$arg1,$arg2,$arg++);

1. $format represents the string and rules to format the variables inside the string. It is mandatory. It has 15 possible values.

  • %%: It returns a percent sign.
  • %b: It used for binary numbers.
  • %c: It used to format characters according to ASCII values.
  • %e: It used for the scientific notation in lowercase.
  • %E: Scientific notation in uppercase.
  • %u: Negative unsigned decimal numbers.
  • %f: Floating point numbers ( local setting aware).
  • %F: Floating point numbers (Not local setting aware).
  • %g: Shorter of %e and %f.
  • %G: Shorter of %E and %f.
  • %o: Octal numbers.
  • %s: String
  • %x: Lowercased hexadecimal numbers.
  • %X: Uppercased hexadecimal numbers.
  • %d: Signed decimal numbers (Negative, Zero or Positive)

2. $arg1 represents an argument which inserted at the first formatted string. It is mandatory.

3. $arg2 represents an argument which inserted at the second formatted string. It is optional.

4. $arg++ is also optional and its inserted at the next formatted string.


Example 1:

<?php
$number =570350.23456;
printf("%d",$number);  // using %d to return integer from floating value
?>

Output:


Example 2:

<?php
$number =570350;
printf("%f",$number); // using %f to return floating number from integer number
?>

Output:



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments