如何找到值并将其用于PHP?

发布于 2025-02-12 19:50:31 字数 1968 浏览 1 评论 0原文

我的目标是这样做:

  1. 从变量中获取值,
  2. 值,从变量检查值
  3. 根据
  4. 使用新值的

更改为数千个。示例:

$input = 12345

//Code to detect input is in thousands or hundreds 
$input = 12345 ==> $change = 12000 / 10000

我的目标是实现$变更,代码可以检测输入并返回该代码是否为数百或数千个等等。

在结合了所有意见之后,我得到的答案是:

$maxBarChart= max($countBarChart1,$countBarChart2,$countBarChart3); //The inputs
$length = strlen(strval($maxBarChart)); // Get the length

//Checks
if($length==1){
       $numberBarChart = floor($maxBarChart/1);
       $totalBarChart = ($numberBarChart*1)+5;
}
elseif($length==2){                                                
       $numberBarChart = floor($maxBarChart/10);
       $totalBarChart = ($numberBarChart*10)+50;    
}
elseif($length==3){
       $numberBarChart = floor($maxBarChart/100);
       $totalBarChart = ($numberBarChart*100)+500;    
}
elseif($length==4){
       $numberBarChart = floor($maxBarChart/1000);
       $totalBarChart = ($numberBarChart*1000)+5000;    
}
elseif($length==5){
       $numberBarChart = floor($maxBarChart/10000);
       $totalBarChart = ($numberBarChart*10000)+50000;    
}
elseif($length==6){
       $numberBarChart = floor($maxBarChart/100000);
       $totalBarChart = ($numberBarChart*100000)+500000;    
}
elseif($length==7){
       $numberBarChart = floor($maxBarChart/1000000);
       $totalBarChart = ($numberBarChart*1000000)+5000000;    
}
elseif($length==8){
       $numberBarChart = floor($maxBarChart/10000000);
       $totalBarChart = ($numberBarChart*10000000)+50000000;    
}
elseif($length==9){
       $numberBarChart = floor($maxBarChart/100000000);
       $totalBarChart = ($numberBarChart*100000000)+500000000;    
}
elseif($length==10){
       $numberBarChart = floor($maxBarChart/1000000000);
       $totalBarChart = ($numberBarChart*1000000000)+5000000000;    
}

尽管这是粗略的和效率低下的意见,但是这些代码奏效并实现了我的目标,但该帖子却是为了了解地板功能和strlen的想法。

谢谢所有建议并回答我帖子的人。

My goal is to do this :

  1. Get the value from variable
  2. Check the value
  3. Change to thousands depending on the value
  4. Use the new value.

Example :

$input = 12345

//Code to detect input is in thousands or hundreds 
$input = 12345 ==> $change = 12000 / 10000

My Goal is to achieve $change, where the code could detect the input and return whether or not it's in hundreds or thousands and so forth.

The Answer I arrived in after combining all of your opinions :

$maxBarChart= max($countBarChart1,$countBarChart2,$countBarChart3); //The inputs
$length = strlen(strval($maxBarChart)); // Get the length

//Checks
if($length==1){
       $numberBarChart = floor($maxBarChart/1);
       $totalBarChart = ($numberBarChart*1)+5;
}
elseif($length==2){                                                
       $numberBarChart = floor($maxBarChart/10);
       $totalBarChart = ($numberBarChart*10)+50;    
}
elseif($length==3){
       $numberBarChart = floor($maxBarChart/100);
       $totalBarChart = ($numberBarChart*100)+500;    
}
elseif($length==4){
       $numberBarChart = floor($maxBarChart/1000);
       $totalBarChart = ($numberBarChart*1000)+5000;    
}
elseif($length==5){
       $numberBarChart = floor($maxBarChart/10000);
       $totalBarChart = ($numberBarChart*10000)+50000;    
}
elseif($length==6){
       $numberBarChart = floor($maxBarChart/100000);
       $totalBarChart = ($numberBarChart*100000)+500000;    
}
elseif($length==7){
       $numberBarChart = floor($maxBarChart/1000000);
       $totalBarChart = ($numberBarChart*1000000)+5000000;    
}
elseif($length==8){
       $numberBarChart = floor($maxBarChart/10000000);
       $totalBarChart = ($numberBarChart*10000000)+50000000;    
}
elseif($length==9){
       $numberBarChart = floor($maxBarChart/100000000);
       $totalBarChart = ($numberBarChart*100000000)+500000000;    
}
elseif($length==10){
       $numberBarChart = floor($maxBarChart/1000000000);
       $totalBarChart = ($numberBarChart*1000000000)+5000000000;    
}

Although it's crude and inefficent but the code works and achieved what I was aiming for, the post was made to find out about the floor function and strlen idea.

Thank You Everyone who advised and answered my post.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以往的大感动 2025-02-19 19:50:31

使用 floor()函数并除以1000,以获取成千上万的输入数:

$change = floor($input/1000) //12

相同的方式,您获得数百个数字:

$change = floor($input/100) //123

如果结果为0(输入为143,则例如),这意味着其中没有千。您应该除以10,然后用100尝试一下:

if(floor($input/1000) == 0) {

    if(floor($input/100 == 0)) {

        //...

    }

} else {

    $change = floor($input/1000);

}

您可以以 /10的步骤进行,以尝试数千,数百等。

如果您需要获得其余的,请使用Modulo操作员。

例如,120%100 = 20。此外,12345%10000 = 2345。

Use the floor() function and divide by 1000 to get the number of thousands in your input :

$change = floor($input/1000) //12

The same way, you get the number of hundreds :

$change = floor($input/100) //123

If the result is 0 (with an input of 143 for example), that means there is no thousand in it. You should divide by 10 and try it out with 100 :

if(floor($input/1000) == 0) {

    if(floor($input/100 == 0)) {

        //...

    }

} else {

    $change = floor($input/1000);

}

You may do the same into a for loop with a step of /10 to try thousands, hundreds etc.

If you need to get the rest, use the modulo operator.

For example, 120 % 100 = 20. Also, 12345 % 10000 = 2345.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文