Home > Backend Development > PHP Problem > Analysis of sprintf() function in php (with code example)

Analysis of sprintf() function in php (with code example)

autoload
Release: 2023-03-09 14:08:01
Original
3109 people have browsed it

Analysis of sprintf() function in php (with code example)

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
Copy after login

$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>";
Copy after login
输出:变量a二进制为:1010;
      变量a八进制为:12;
      变量a十进制为:10;
      变量a十六进制为:12;
      变量b字符串为:php.cn
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template