gpt4 book ai didi

php - 检查变量是否以结尾

转载 作者:行者123 更新时间:2023-12-02 07:01:01 24 4
gpt4 key购买 nike

我有变量$uid总是包含一个数字。我想根据 $uid 的结束方式执行不同的操作。例如:

if(&uid ENDS WITH 2,3 or 4) {echo "Some content"};
else if($uid ENDS WITH 1,5 or 6) {echo "Some other content"};
else if($uid ENDS WITH 7,8,9 or 0) {echo "Some other content"};

我没有想到如何实现这一点。提前感谢您的宝贵时间!

最佳答案

有几种方法可以摆动这只猫。像你一样使用大量的 if/elseif,但这变得难以阅读。您可以使用 in_array(),这是一个很好的解决方案,但同样您有多个 if/elseifs。

我更喜欢使用开关。

$end=substr($uid, -1);

switch ($end){
case 2:
case 3:
case 4:
echo "Some content";
break;
case 1:
case 5:
case 6:
echo "Some other content";
break;
case 7:
case 8:
case 9:
case 0:
echo "Some other other content";
break;
}

关于php - 检查变量是否以结尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20763511/

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