現在の言語: 日本語

戻る

null型
php型宣言

null型は変数が値を持っていないことを示す特別な型です。
値の欠如や未定義をの状態を表現するために使用します。

[自動的にnullになるケース]
宣言のみで初期値が設定されていない変数
unset()関数で破棄された変数
値を返さない関す(return;またはreturn null;)の返り値
nullの書き方
大文字・小文字の区別はありません。
NULL
null
Null
いずれも同じ意味として扱われます。
一般的には小文字のnullが推奨されているようです。

[サンプル]

copy
class test2
{
  //関数の引数に何も入れずに実行するとエラーになります
  //Fatal error:  Uncaught Error: Call to undefined method test2::tes1() 
  function test1(string $data)
  {
     echo "test1:".$data.PHP_EOL;
  }
  //null許容型を設定したケース
  function test2(?string $data)
  {
    if($data === null)
    {
      echo "test2:null".PHP_EOL;
    }
    else
    {
      echo "test2(not null):".$data.PHP_EOL;
    }
  }
  function test3(?string $data = null)
  {
    if($data === null)
    {
      echo "test3:null".PHP_EOL;
    }
    else
    {
      echo "test3(not null):".$data.PHP_EOL;
    }
  }
  function test4(?string $data)
  {
    if(is_null($data) === true)
    {
      echo "test4:null".PHP_EOL;
    }
    else
    {
      echo "test4(not null):".$data.PHP_EOL;
    }
  }
  function test5(string $data)
  {
    if($data === true)
    {
      echo "test5:null".PHP_EOL;
    }
    else
    {
      echo "test5(not null):".$data.PHP_EOL;
    }
  }
}
echo "<pre>";

$cls2 = new test2();

//$cls2->test1();
//[実行結果]
//関数の引数に何も入れずに実行するとエラーになります
//Fatal error:  Uncaught ArgumentCountError: Too few arguments to function test2::test1(), 0 passed 

//$cls2->test2();
//[実行結果]
//関数の引数に何も入れずに実行するとエラーになります
//Fatal error:  Uncaught ArgumentCountError: Too few arguments to function test2::test2(), 0 passed


$cls2->test2(null);//test2:null

$data = null;//test2:null
$cls2->test2($data);

$cls2->test2("test");//test2(not null):test

$cls2->test3();//test3:null
$cls2->test3($data);//test3:null
$cls2->test3("test");//test3(not null):test
$cls2->test4($data);//test4:null
//Fatal error: Uncaught TypeError: test2::test5(): Argument #1 ($data) must be of type string, null $cls2->test5($data);
given, called
echo "</pre>";
copy
class test2
{
	//An error will occur if the function is executed without any arguments.
	//Fatal error: Uncaught Error: Call to undefined method test2::tes1()
	function test1(string $data)
	{
	echo "test1:".$data.PHP_EOL;
	}
	//Example when a nullable type is specified
	function test2(?string $data)
	{
		if($data === null)
		{
			echo "test2:null".PHP_EOL;
		}
		else
		{
			echo "test2(not null):".$data.PHP_EOL;
		}
	}
	function test3(?string $data = null)
	{
		if($data === null)
		{
			echo "test3:null".PHP_EOL;
		}
		else
		{
			echo "test3(not null):".$data.PHP_EOL;
		}
	}
	function test4(?string $data)
	{
		if(is_null($data) === true)
		{
			echo "test4:null".PHP_EOL;
		}
		else
		{
			echo "test4(not null):".$data.PHP_EOL;
		}
	}
	function test5(string $data)
	{
		if($data === true)
		{
			echo "test5:null".PHP_EOL;
		}
		else
		{
			echo "test5(not null):".$data.PHP_EOL;
		}
	}
}
echo "<pre>";

$cls2 = new test2();

//$cls2->test1();
//[Execution result]
//An error occurs if the function is executed without any arguments.
//Fatal error: Uncaught ArgumentCountError: Too few arguments to function test2::test1(), 0 passed

//$cls2->test2();
//[Execution result]
//An error occurs if the function is executed without any arguments.
//Fatal error: Uncaught ArgumentCountError: Too few arguments to function test2::test2(), 0 passed

$cls2->test2(null);//test2:null

$data = null;//test2:null
$cls2->test2($data);

$cls2->test2("test");//test2(not null):test

$cls2->test3();//test3:null
$cls2->test3($data);//test3:null
$cls2->test3("test");//test3(not null):test
$cls2->test4($data);//test4:null
//Fatal error: Uncaught TypeError: test2::test5(): Argument #1 ($data) must be of type string, null $cls2->test5($data);
given, called
echo "</pre>";

$cls2->test2(null);
型も含めて厳密に比較します。

function test3(?string $data = null)
?(null許容型)は関数の引数に型宣言をした左につけると
nullも受け入れるような仕組みとなります。

if(is_null($data) === true)
is_null関数は、変数が厳密にnullであるかをチェックします。

php ver7.0
型宣言(Type Hinting)が標準でより厳密化されました。

function test5(string $data)
上記関数に下記のように変数にnullを準備して関数を実行します。
※試した環境:php8.2
$data = null;
test5($null);
[結果]
Fatal error: Uncaught TypeError: test2::test5(): Argument #1 ($data) must be of type string, null
null許容型をつけないで引数にnullの入った変数を入れて実行すると上記のようにエラーになります。



copy
class test2
{
  //関数の引数に何も入れずに実行するとエラーになります
  //Fatal error:  Uncaught Error: Call to undefined method test2::test1() 
  function test1(string $data)
  {
     echo "test1:".$data.PHP_EOL;
  }
  //null許容型を設定したケース
  function test2(?string $data)
  {
    if($data === null)
    {
      echo "test2:null".PHP_EOL;
    }
    else
    {
      echo "test2(not null):".$data.PHP_EOL;
    }
  }
  function test3(?string $data = null)
  {
    if($data === null)
    {
      echo "test3:null".PHP_EOL;
    }
    else
    {
      echo "test3(not null):".$data.PHP_EOL;
    }
  }
}
echo "<pre>";

$cls2 = new test2();

//$cls2->test1();
//[実行結果]
//関数の引数に何も入れずに実行するとエラーになります
//Fatal error:  Uncaught ArgumentCountError: Too few arguments to function test2::test1(), 0 passed 

//$cls2->test2();
//[実行結果]
//関数の引数に何も入れずに実行するとエラーになります
//Fatal error:  Uncaught ArgumentCountError: Too few arguments to function test2::test2(), 0 passed


$cls2->test2(null);//test2:null

$data = null;//test2:null
$cls2->test2($data);

$cls2->test2("test");//test2(not null):test

$cls2->test3();//test3:null
$cls2->test3($data);//test3:null
$cls2->test3("test");//test3(not null):test
echo "</pre>";
copy
class test2
{
	//An error will occur if the function is executed without any arguments.
	//Fatal error: Uncaught Error: Call to undefined method test2::test1()
	function test1(string $data)
	{
		echo "test1:".$data.PHP_EOL;
	}
	//Example when a nullable type is specified
	function test2(?string $data)
	{
		if($data === null)
		{
			echo "test2:null".PHP_EOL;
		}
		else
		{
			echo "test2(not null):".$data.PHP_EOL;
		}
	}
	function test3(?string $data = null)
	{
		if($data === null)
		{
			echo "test3:null".PHP_EOL;
		}
		else
		{
			echo "test3(not null):".$data.PHP_EOL;
		}
	}
}
echo "<pre>";

$cls2 = new test2();

//$cls2->test1();
//[Execution Result]
//An error occurs if the function is executed without any arguments.
//Fatal error: Uncaught ArgumentCountError: Too few arguments to function test2::test1(), 0 passed

//$cls2->test2();
//[Execution Result]
//An error occurs if the function is executed without any arguments.
//Fatal error: Uncaught ArgumentCountError: Too few arguments to function test2::test2(), 0 passed

$cls2->test2(null);//test2:null

$data = null;//test2:null
$cls2->test2($data);

$cls2->test2("test");//test2(not null):test

$cls2->test3();//test3:null
$cls2->test3($data);//test3:null
$cls2->test3("test");//test3(not null):test
echo "</pre>";
function test3(?string $data = null)
引数に何も入れずに実行するとデフォルトでnullが設定される例です。

if($data === null)
nullに対して厳密化するような仕組みになっているphpで
このような方法でnullおよびデータがない場合に対して
nullかどうか?を判定して
仕様に応じた判定処理を実装します。



戻る

著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。