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