Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

sscanf()

Read fields from text according to a format.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Read fields from text according to a format. Without output variables the results form an array, with null for an unmatched expected field. With reference variables the function assigns them and returns the count assigned on a successful or partial scan. Check the count before using all requested fields. End of input before any assignment returns null without references, or -1 with references left untouched. The wrong number of output variables raises ValueError.

Signature

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

Example output

[[12,null],2,7,21]
[null,-1,9]

Examples

<?php
$fields=sscanf("12 x","%d %d"); $a=$b=null;
$count=sscanf("7 21","%d %d",$a,$b);
echo json_encode([$fields,$count,$a,$b]);
$a=9; echo "\n", json_encode([sscanf("","%d"),sscanf("","%d",$a),$a]);

Result

[[12,null],2,7,21]
[null,-1,9]

Sources