How to implement replacement in php sprintf

藏色散人
Release: 2023-03-14 14:28:01
Original
2200 people have browsed it

php sprintf method to implement replacement: 1. Create a PHP sample file; 2. Pass the specified value through "sprintf("There are %u million cars in %s.",$number,$str);" Just replace the symbols.

How to implement replacement in php sprintf

The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.

php sprintf How to implement replacement?

sprintf() function writes a formatted string into a variable.

arg1, arg2, parameters will be inserted into the main string at the percent sign (%) symbol. This function is executed step by step. At the first % sign, insert arg1, at the second % sign, arg2, and so on.

Note: If there are more % symbols than arg arguments, you must use placeholders. The placeholder follows the % symbol and consists of a number and "\$". See example 2.

Tips: Related functions: printf(), vprintf(), vsprintf(), fprintf() and vfprintf()

Example

The percent sign (%) symbol is replaced with a variable passed as a parameter:

<?php
$number = 2;
$str = "Shanghai";
$txt = sprintf("There are %u million cars in %s.",$number,$str);
echo $txt;
?>
Copy after login

Running result:

There are 2 million cars in Shanghai.
Copy after login

sprintf syntax:

sprintf(format,arg1,arg2,arg)

How to implement replacement in php sprintf

## Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to implement replacement in php sprintf. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!