LoginSignup
18
22

More than 5 years have passed since last update.

Notice: Undefined variable:

Last updated at Posted at 2015-06-02

Notice: Undefined variable:エラー

案外遭遇するのでメモ。

どういうときに出てくるエラーなのかというと↓

IndexController.php
$value = 5;

if($value == 12){
  $ans = "valueは、12です。";
}

$this -> view -> answer = $ans;

このとき $value は if の中を通らないので、

$this -> view -> answer = $ans;

のところでエラーが出る。
「$ansって何やねん」ってことで、「Notice: Undefined variable:」なのね。

こう直してあげれば怒られないはず↓

IndexController.php
$value = 5;
$ans = null;  //初期化してあげるとか。

if($value == 12){
  $ans = "valueは、12です。";
}else{
 $ans = "valueは12ではないです。";  //絶対値が入るようにしてあげるとか。
}

$this -> view -> answer = $ans;
18
22
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
18
22