PHP sprintf
Insert a student or parcel identifier into a reusable display template.
A simple example
sprintf
- Start with Text A, template [value]
- Do this Insert text between brackets
- Result "[A]"
Syntax
sprintf("ID [%s]", $value)
Run these examples in PHP
Save one example at a time as lesson.php. Use PHP 8.2 or later.
php lesson.phpPrint a student badge
A printer receives badge or ticket data in separate fields. Use the display format required by the desk so names, numbers and separators occupy their intended places on the finished label.
<?php
$value = "Ada";
echo (sprintf("ID [%s]", $value)), PHP_EOL;
ID [Ada]
The brackets come from the template.
Print a parcel badge
A parcel badge combines data supplied by the shipping system into one printable line. Format those fields in the order the warehouse worker needs to read them during a handoff.
<?php
$value = "PK-17";
echo (sprintf("ID [%s]", $value)), PHP_EOL;
ID [PK-17]
The identifier is inserted unchanged.
Print the first entry tag
A coat-check desk numbers the first ticket one. The ticket printer includes the number as ordinary decimal text after a prefix. What does the completed label say?
What to watch for
Treat the template as application code and pass user-provided values as arguments.