为什么我无法在PHP中的foreach循环中设置变量?

问题描述:

I'm trying to set variables inside a foreach() statement, but it keeps dying.

If I do this, all is fine.

foreach($array as $key => $value)
    {
         echo '<tr>';
            echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' . $value['1'] . '">' . $value['1'] . '</a></td>';
         echo '</tr>';
    }

But when I do this, it doesn't want to work.

foreach($array as $key => $value)
    {
        $mls = echo '' . $value['1'] . '';
         echo '<tr>';
            echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' $mls '">' $mls '</a></td>';
         echo '</tr>';
    }

Syntax wise, I don't see how there's a difference in these statements. I've also tried $mls = $value['1']; and that didn't want to work either.

我正在尝试在 foreach() code>语句中设置变量,但它保持不变 死了。 p>

如果我这样做,一切都很好。 p>

  foreach($ array as $ key =&gt; $ value)\  n {
 echo'&lt; tr&gt;'; 
 echo'&lt; td&gt;&lt; a href =“http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum='。  $ value ['1']。'“&gt;'  。  $ value ['1']。  '&lt; / a&gt;&lt; / td&gt;'; 
 echo'&lt; / tr&gt;'; 
} 
  code>  pre> 
 
 

但是当我这样做时, 它不想工作。 p>

  foreach($ array as $ key =&gt; $ value)
 {
 $ mls = echo''。  $ value ['1']。  ''; 
 echo'&lt; tr&gt;'; 
 echo'&lt; td&gt;&lt; a href =“http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum='  $ mls'“&gt;'  $ mls'&lt; / a&gt;&lt; / td&gt;'; 
 echo'&lt; / tr&gt;'; 
} 
  code>  pre> 
 
 

语法明智,我 不要看这些陈述有何不同。 我也尝试过 $ mls = $ value ['1']; code>,但也不想这么做。 p> div>

Surely you got a syntax error complaining about the second case, right? If you say "it keeps dying", you should tell us exactly what happens when something dies. Even more, you should read the syntax error and consider what it says. The errors are descriptive so that you can figure out what's wrong.

In the second case, you aren't concatenating the strings with the . operator.

echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' $mls '">' $mls '</a></td>';

should be

echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' . $mls . '">' . $mls . '</a></td>';

 $mls = echo '' . $value['1'] . '';

should be

$mls =   $value['1'] ;
echo $mls;

and

  echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' $mls '">' $mls '</a></td>'

should be

echo '<td><a href="http://mgoode.com/index.php?option=com_mls&view=mls&mlsnum=' . $mls . '">' . $mls . '</a></td>';

Your second code block should look more like this:

foreach($array as $key => $value)
{
    $mls = (string) $value['1'];
    echo '<tr>';
    echo '<td><a href="http://mgoode.com/index.php?option=com_mls&amp;view=mls&mlsnumamp;=' , $mls , '">'  , $mls , '</a></td>';
    echo '</tr>';
}
  • When you type $var = echo "something" you aren't assigning any values to that variable. Instead you are outputting that string - echo has no return value.
  • You can typecast your variable into a string without appending and prepending empty strings.
  • You can use the , to echo multiple strings one after another with a little less overhead.
  • You should be using HTML entities for your ampersands, even though they are in an attribute's value
  • (Finally) You aren't actually concatenating your variable into the third echo.