gpt4 book ai didi

php - 如何为网站访问者打印 0000000 值

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:25 24 4
gpt4 key购买 nike

我在 php 中使用网站计数器,我只需要显示从 0000000 值开始的计数器。

下面是我的代码:

<?php
//opens countlog.txt to read the number of hits

$datei = fopen("countlog.txt","r");
$count = fgets($datei,1000);
fclose($datei);
$count=$count + 1 ;
// get the total number of digits in the counter
$number_length = strlen($count);
$main_image = "";
for($i=0;$i<$number_length;$i++){
$digit = substr($count, $i, 1);
if($digit == 1){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/1.png'>";
}
if($digit == 2){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/2.png'>";
}
if($digit == 3){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/3.png'>";
}
if($digit == 4){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/4.png'>";
}
if($digit == 5){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/5.png'>";
}
if($digit == 6){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/6.png'>";
}
if($digit == 7){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/7.png'>";
}
if($digit == 8){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/8.png'>";
}
if($digit == 9){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/9.png'>";
}`enter code here`
if($digit == 0){
$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/0.png'>";
}
}
echo $main_image;
// opens countlog.txt to change new hit number
$datei = fopen("countlog.txt","w");
fwrite($datei, $count);
fclose($datei);

?>

最佳答案

试试 str_pad 函数:

$count=$count + 1 ;
$count = str_pad($count, 7, '0', STR_PAD_LEFT);

你也可以用一行替换你的大if block :

$main_image .= "<img height='32' width='19' src='".SITE_URL."images/digit/".$digit.".png'>";

关于php - 如何为网站访问者打印 0000000 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46337560/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com