值为1后循环停止的PHP

值为1后循环停止的PHP,php,Php,我有以下for循环,它使用变量并在xml输出中显示相关匹配项非常有效。我只想显示第一个匹配项(例如_1.jpg),而不是循环并显示整个$pic\u count 我已尝试删除++并更改上的数学值等。如何仅显示一的$picu start值,然后退出for循环 for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) { echo '<photo isMain="1"><photoSmall><!

我有以下for循环,它使用变量并在xml输出中显示相关匹配项非常有效。我只想显示第一个匹配项(例如_1.jpg),而不是循环并显示整个
$pic\u count

我已尝试删除
++
并更改
上的数学值等。如何仅显示一的
$picu start
值,然后退出for循环

for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
  echo '<photo isMain="1"><photoSmall><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoSmall><photoLarge><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoLarge></photo>';
}
for($pic\u start=1;$pic\u start只需将放在循环的末尾。因此,您的代码如下所示

for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
echo '<photo isMain="1"><photoSmall><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoSmall><photoLarge><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoLarge></photo>';
break; // will execute loop only once.
}
for($pic\u start=1;$pic\u start)
中断结束当前for、foreach、while、do while或的执行
开关结构

我们使用break命令在特定条件下中断循环,在您的情况下,我们只需要在第一次执行后中断循环。因此将其放在循环的末尾。

只需将放在循环的末尾。因此,您的代码如下所示

for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
echo '<photo isMain="1"><photoSmall><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoSmall><photoLarge><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoLarge></photo>';
break; // will execute loop only once.
}
for($pic\u start=1;$pic\u start)
中断结束当前for、foreach、while、do while或的执行
开关结构


我们使用break命令在特定条件下中断循环,在您的情况下,我们只需要在第一次执行后中断循环。因此,请将其放在循环的末尾。

只需在循环中使用break:

$stop_value = 2; 
for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
     if ($pic_start == $stop_value) {
         break;
     } 
     echo '<photo isMain="1"><photoSmall><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoSmall><photoLarge><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoLarge></photo>';
}
$stop\u值=2;

对于($pic_start=1;$pic_start只需在循环中使用中断:

$stop_value = 2; 
for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
     if ($pic_start == $stop_value) {
         break;
     } 
     echo '<photo isMain="1"><photoSmall><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoSmall><photoLarge><![CDATA[/feeds/' . $img . '/rets_images/' . $listing . '_' . $pic_start . '.jpg]]></photoLarge></photo>';
}
$stop\u值=2;
对于($pic_start=1;$pic_start请尝试以下操作:

for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
    echo ...;
    break;
}
对于($pic\u start=1;$pic\u start请尝试以下操作:

for ($pic_start = 1; $pic_start <= $pic_count; $pic_start++) 
{
    echo ...;
    break;
}

对于($pic\u start=1;$pic\u start
$pic\u start=1;echo…
一次删除循环如果只想运行一次,为什么还要麻烦循环?
$pic\u start=1;echo…
一次删除循环如果只想运行一次,为什么还要麻烦循环?