LoginSignup
0
0

More than 1 year has passed since last update.

【PHP】なぜ初期化をするのか?

Posted at

コード

<?php
foreach ([1,2,3,4]as $a){
    if($a%2 === 0){
        $b = $a;
    }
    echo $b,PHP_EOL;
}

上記の場合はエラーになる

PHP Warning:  Undefined variable $b

なので

<?php
$b = 0;
foreach ([1,2,3,4]as $a){
    if($a%2 === 0){
        $b = $a;
    }
    echo $b,PHP_EOL;
}

結果

0
2
2
4
0
0
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
0
0