gpt4 book ai didi

php - 在 while 中用 If 语句排序

转载 作者:行者123 更新时间:2023-11-29 00:10:31 25 4
gpt4 key购买 nike

我有一个带有单词的mysql数据库。我用 while 语句打印所有单词。所以我觉得:

马铃薯番茄生菜

一切正常,但我想按长度对单词进行排序。我试过:

if(strlen($go['words']) == 4 ){ echo "this are the 4 letter words:"; }

但这将在每 4 个字母的单词之前打印句子。然而,我希望它只打印 1 次作为所有 4 个字母单词的标题。当然,我也想对 5、6、7 个字母的单词执行此操作。

我考虑过进行多个 sql 查询,但是服务器无法处理大量访问者。

最佳答案

你可以像这样使用一个临时变量:

$tempLen = 0;
while(...)
{
if(strlen($go['words']) == 1 && $tempLen < 1){
echo "this are the 1 letter words:";
$tempLen = 1;
}
...
if(strlen($go['words']) == 4 && $tempLen < 4){
echo "this are the 4 letter words:";
$tempLen = 4;
}
...
}

关于php - 在 while 中用 If 语句排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25342317/

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