PHP Programing language

sprintf() & sscanf() Functions in PHP
Previous Home Next
adplus-dvertising

sprintf()

The sprintf() function writes a formatted string to a variable.

Syntax:
sprintf(format,arg1,arg2,arg++)

Example:

<?php
$number = 9;
$str = sprintf("%d is a number.",$number);
echo $str;
?>

Output:

9 is a number.

sscanf()

The sscanf() function parses input from a string according to a specified format. The sscanf() function parses a string into variables based on the format string.

Syntax:
sscanf(string,format,arg1,arg2,arg++)

Example:

<?php
$str = "age:25 class:1";
sscanf($str,"age:%d class:%d",$age,$class);
// show types and values
var_dump($age,$class);
?>

Output:

int(25) int(1)

Previous Home Next