Home  >  Article  >  Backend Development  >  How to add 0 if there are not enough digits in php?

How to add 0 if there are not enough digits in php?

WBOY
WBOYOriginal
2022-02-11 13:48:552369browse

In PHP, you can use the sprintf() function to supplement the number with insufficient digits with 0. This function is used to write the formatted string into a variable. When the parameter is set to "d ", you can add zeros to 5 digits, the syntax is "sprintf(" d", value)".

How to add 0 if there are not enough digits in php?

The operating environment of this tutorial: windows10 system, PHP7.1 version, DELL G3 computer

How to use 5 digits when there are not enough digits in php 0Supplement

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.

The syntax is:

sprintf(format,arg1,arg2,arg++)

The example is as follows:

<?php
  $s1=sprintf("%05d",1234);
  echo $s1 ,"<br>";
?>

Output result:

How to add 0 if there are not enough digits in php?

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to add 0 if there are not enough digits in php?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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