回声循环 [英] Echo in for loop

查看:129
本文介绍了回声循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

 <?php 
$ date = 2015-02-30;
$ year = substr($ date,0,4);
$ month = substr($ date,5,2);
$ day = substr($ date,8,2);
?>

< select> ($ i = 1; $ i <31; $ i ++){?>

<?php

< option value =<?php echo $ i;?> <?php if($ day === $ i){echoselected; }; ?>><?php echo $ i; ?>< /选项>
<?php}?>

< / select>

对于该选项,应该选择数字4。为什么不起作用?谢谢

对不起,我已经在select语句中有这个了

编辑:看上面的代码编辑。也许是因为

解决方案

您需要将代码包装在 select 语句中!



如果没有 select 标记,选项语句将无法正常工作:

 < HTML> 

< body>

< select> <! - 启动select语句 - >

<! - 您的代码 - >
<?php
$ num = 4;
for($ i = 1; $ i <10; $ i ++)
{
?>
< option value =<?php echo $ i;?> <?php if($ num === $ i){echoselected; }; ?>><?php echo $ i; ?>< /选项>
<?php
}
?>
<! - 结束您的代码 - >

< / select> <! - 结束选择语句 - >

< / body>

< / html>


This is my code:

<?php
$date = 2015-02-30;
$year = substr($date, 0, 4);
$month = substr($date, 5, 2);
$day = substr($date, 8, 2);
?>

<select>

<?php
for ($i=1; $i < 31; $i++) { ?>
    <option value="<?php echo $i; ?>" <?php if($day === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
<?php } ?>

</select>

For the option, the number 4 should be selected. Why doesn't it work? Thanks

Sorry, I already had this in a select statement

EDIT: See the code edit above. Maybe because the

解决方案

You need to wrap your code in a select statement!

An option statement wont work without the select tag around it:

<html>

<body>

<select>  <!-- Start the select statement -->

<!-- Your Code -->
<?php
$num = 4;
for ($i=1; $i < 10; $i++)
{
    ?>
    <option value="<?php echo $i; ?>" <?php if($num === $i){ echo "selected"; }; ?>><?php echo $i; ?></option>
    <?php
}
?>
<!-- End your code -->

</select> <!-- End the select statement -->

</body>

</html>

这篇关于回声循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆