问题描述
我正在从"Murach PHP and MySQL 2nd Edition"一书中学习PHP,并且遇到了我不完全理解的这段代码...
I am learning PHP from the book "Murach PHP and MySQL 2nd Edition" and I came across this code which I don't fully understand...
<select name="productkey">
<?php foreach ($products as $key => $product):
$cost = number_format($product['cost'], 2);
$name = $product['name'];
$item = $name . ' ($)' . $cost . ')';
?>
<option value="<?php echo $key; ?>"> <?php echo $item; ?></option>
<?php endforeach; ?>
</select>
为什么在foreach循环开始时在冒号(:)后面有指令?
Why are there instructions after the colon (:) at the start of the foreach loop??
我读到冒号后面应该有?>",然后是要执行的指令,然后在"末尾标记foreach循环的结尾...
I read that after the colon there should be "?>" and then the instructions to execute and then at end "" to mark the end of the foreach loop...
请对此进行解释,并且如果我在<?php foreach ($products as $key => $product): ?>
之后写以$ cost,$ name和$ item开头的行,代码仍然可以正常工作吗?
Please explain this AND IF I WRITE THE LINES STARTING WITH $cost,$name and $item after <?php foreach ($products as $key => $product): ?>
would the code still work???
推荐答案
是的,它仍然可以工作:与{相同,但是您必须在foreach的末尾写endforeach,它做同样的事情.页 文档
yes it would still work : is the same as { but you have to write endforeach at the end of your foreach it does the same thing more info check out this page documentation
这篇关于PHP foreach备用语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!