LoginSignup
4
4

More than 5 years have passed since last update.

while文を使って「任意に繰り返しを止める」

Posted at

実際に以下

2012年2月の日付を表示してみる。

① 2つの括弧()と{}をもつwhile文のテンプレートをよういする。

<?php
while () {
}
?> 

② {}の中に日付($d)を改行しながら表示するコードを記述する

<?php
while () {
    echo $d . "<br />";
}
?> 

③ checkdate関数を使って、while分の終了条件を()の中に記述。$dが1、2、3・・・の時は妥当な日付としてtrueが返されるので、()の中もtrueとなり、while文による日付表示が繰り返される。$dが30(2012年2月30日)になると、「checkdate(2,30,2012)」という日時チェックがfalseを返し、while文が終了する。

<?php
$d = 1;

while (checkdate(2, $d, 2012)) {
    echo $d . "<br />";
    $d++;
}
?> 

以下実行結果↓
php_008.png

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4