In the previous article we introduced "Two ways to generate random numbers in php", this article will introduce the sprintf()
function . PHP may need to include multiple variables continuously when outputting a string. If you use echo
directly, you need to splice each variable and the string, which will be troublesome, so we can use sprintf()
function to solve this problem.
First let’s take a look at the syntax of the sprintf()
function:
sprintf ( string $format , $arg1 ) : string
$format: a string containing placeholders.
$arg1: The corresponding variable in the string.
Code example:
<?php $a=10; $b="php.cn"; echo sprintf("变量a二进制为:%b;",$a); echo "<br>"; echo sprintf("变量a八进制为:%o;",$a); echo "<br>"; echo sprintf("变量a十进制为:%d;",$a); echo "<br>"; echo sprintf("变量a十六进制为:%o;",$a); echo "<br>"; echo sprintf("变量b字符串为:%s",$b); echo "<br>";
输出:变量a二进制为:1010; 变量a八进制为:12; 变量a十进制为:10; 变量a十六进制为:12; 变量b字符串为:php.cn
Recommended: 《2021 PHP interview questions summary (collection) 》《php video tutorial》
The above is the detailed content of Analysis of sprintf() function in php (with code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

WebStorm Mac version
Useful JavaScript development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Linux new version
SublimeText3 Linux latest version
