What is the usage of php sprintf
php The function of sprintf function is to write the formatted string into a variable. Its usage syntax is "sprintf(format,arg1,arg2,arg)". The parameter format specifies the string and how to format it. Variables.
The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer
php What is the usage of sprintf?
sprintf() function writes the 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.
Tip: Related functions: printf(), vprintf(), vsprintf(), fprintf() and vfprintf()
fprintf() printf() vfprintf() vprintf() vsprintf()
Syntax
sprintf(format,arg1,arg2,arg++)
Parameters format is required. Specifies a string and how to format variables within it.
Possible format values:
%% - returns a percent sign %
%b - a binary number
%c - the corresponding ASCII value Characters
%d - Decimal number with sign (negative, 0, positive)
%e - Scientific notation using lowercase (e.g. 1.2e 2)
%E - Use uppercase scientific notation (e.g. 1.2E 2)
%u - Decimal number without sign (greater than or equal to 0)
%f - Floating point number (local setting)
%F - floating point number (non-local setting)
%g - shorter %e and %f
%G - shorter %E and %f
%o - octal number
%s - string
%x - hexadecimal number (lowercase letters)
%X - Hexadecimal number (capital letters)
Additional format value. Must be placed between % and letters (such as %.2f):
(Add or - in front of the number to define the sign of the number. By default, only negative numbers are marked, positive numbers are not mark)
' (Specifies what to use as padding, defaults to spaces. It must be used with a width specifier. For example: %'x20s (use "x" as padding))
- (Left-adjusted variable value)
[0-9] (Specifies the minimum width of the variable value)
.[0-9] (Specifies the number of decimal places or the maximum string length)
Note: If multiple above format values are used, they must be used in the above order.
arg1 Required. Specifies the parameters to be inserted at the first % sign in the format string.
arg2 Optional. Specifies the parameter to be inserted into the format string at the second % sign.
arg Optional. Specifies the parameters to be inserted at the third, fourth, etc. % symbols in the format string.
Example
Replace the percent sign (%) symbol 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; ?>
Running result:
There are 2 million cars in Shanghai.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of What is the usage of php sprintf. 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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,
