PHP sscanf() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, sscanf() reads values out of a string using a format template.

It is the opposite of sprintf(). It can return an array or fill variables you pass in.

Syntax

Call sscanf() with the values you need. Start with this line.

PHP
$parts = sscanf("2026-11-11", "%d-%d-%d");
CallWhat it does
sscanf("id=42", "id=%d")Returns [42].
sscanf($s, $format, $a, $b)Fills $a and $b and returns how many were set.

Without extra variables, sscanf() returns an array. With variables, it returns the number of values assigned.

Library declarations
PHP
sscanf(string $string, string $format, mixed &...$vars): array|int|null

Parameters

ParameterDescription
stringRequired. The input string to read from.
formatRequired. The template, using placeholders such as %d, %s, %f, and %x.
varsOptional. Variables passed by reference that receive the parsed values.

Return Value

Return ValueDescription
arrayThe parsed values when no variables are passed.
intThe number of values assigned when variables are passed.
nullReturned in some cases when the input ends before any value is read.

PHP Examples

Example 1 — Read Numbers from a Date

Pull the year, month, and day out of a string.

PHP
<?php
list($year, $month, $day) = sscanf("2026-11-11", "%d-%d-%d");

echo "Year: ", $year, PHP_EOL;
echo "Month: ", $month, PHP_EOL;
echo "Day: ", $day, PHP_EOL;

How It Works

  1. Write a matching format. Each %d reads a whole number.
  2. Keep the dashes. Plain characters must match the input.
  3. Get an array. list() splits it into three variables.
  4. Print the parts. Each value is already an integer.

Example 2 — Mix Hex, Float, and Word

Different placeholders return different types.

PHP
<?php
$result = sscanf("ff 3.5 hello", "%x %f %s");

print_r($result);

How It Works

  1. %x reads hex. ff becomes 255.
  2. %f reads a float. 3.5 stays a decimal.
  3. %s reads a word. It stops at the next space.
  4. Check the array. Values come back in order.

Example 3 — Assign Directly to Variables

Pass variables and sscanf() fills them for you.

PHP
<?php
$count = sscanf("id=42 score=9.5", "id=%d score=%f", $id, $score);

echo "Assigned: ", $count, PHP_EOL;
echo "ID: ", $id, PHP_EOL;
echo "Score: ", $score, PHP_EOL;

How It Works

  1. Add variables at the end. They are passed by reference.
  2. Match the text. id= and score= are literal text.
  3. Get a count. The return value is how many were assigned.
  4. Use the variables. $id and $score are ready.

Frequently Asked Questions

sscanf() reads values out of a string using a format template. It is the reverse of sprintf().
sscanf(string $string, string $format, mixed &...$vars). The extra variables are optional. Without them, you get an array.
It returns an array of parsed values if you pass no variables. If you pass variables, it returns how many values were assigned.
%s stops at the first whitespace. To read more text, use a character set like %[^\n] or use another function such as explode().
Parsing stops at the mismatch. The remaining values are null in the array or are not assigned to variables.

Next: str_contains()

str_contains() checks whether a string contains another string.

str_contains() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful