現在の言語: 日本語 |
int型 |
php型宣言 |
class test1
{
function judgeInt(int $num)
{
return $num;
}
}
$cls1 = new test1();
$cls1->judgeInt("123");
$cls1->judgeInt(1.23);
//下記は上記クラスとは別テストと仮定します
$value = 1;
$value = null; // 警告なし、値は内部的に 0 として扱われる
echo "value:".(int)$value." <br>";
class test1
{
function judgeInt(int $num)
{
return $num;
}
}
$cls1 = new test1();
$cls1->judgeInt("123");
$cls1->judgeInt(1.23);
//Assume the following is a separate test from the above class.
$value = 1;
$value = null; // No warning, the value is internally treated as 0.
echo "value:".(int)$value." <br>";
| 環境 | サイズ(ビット) | 最大値(PHP_INT_MAX) |
| 32bit | 32 | 2,147,483,647 (約 21億) |
| 64bit | 64 | 9,223,372,036,854,775,807 (約 900京) |
| 環境 | サイズ(ビット) | 最小値(PHP_INT_MIN) |
| 64bit | 64 | -9,223,372,036,854,775,808 (約 -900京) |
class test2
{
private function judgeInt(int $num)
{
echo "judgeInt関数を実行しました。<br>";
}
function test()
{
try {
$this->judgeInt(1.23); // TypeError が発生します
} catch (TypeError $e) {
// ここでエラーを捕捉し、代替処理をします
echo "Caught TypeError: " . $e->getMessage() . PHP_EOL;
// 仕様的には良くないとしても、ここでデフォルト値を返すなどの回避策が可能です
// 例: return round(1.23);
} finally {
echo "Finally block executed (always runs).\n";
}
}
}
$cls2 = new test2();
$cls2->test();
class test2
{
private function judgeInt(int $num)
{
echo "The judgeInt function was executed. <br>";
}
function test()
{
try {
$this->judgeInt(1.23); // A TypeError occurs.
} catch (TypeError $e) {
// Catch the error here and handle it accordingly.
echo "Caught TypeError: " . $e->getMessage() . PHP_EOL;
// Even though this is not technically correct, a workaround is possible, such as returning a default value.
// Example: return round(1.23);
} finally {
echo "Finally block executed (always runs).\n";
}
}
}
$cls2 = new test2();
$cls2->test();
class test3
{
private function judgeInt(int $num)
{
echo "judgeInt関数を実行しました。<br>";
}
function test()
{
try {
$this->judgeInt(null); // Fatal error: Uncaught TypeError
} catch (TypeError $e) {
echo "エラーを捕捉しました: " . $e->getMessage();
}
}
}
$cls3 = new test3();
$cls3->test();
class test3
{
private function judgeInt(int $num)
{
echo "The judgeInt function was executed.<br>";
}
function test()
{
try {
$this->judgeInt(null); // Fatal error: Uncaught TypeError
} catch (TypeError $e) {
echo "Error caught: " . $e->getMessage();
}
}
}
$cls3 = new test3();
$cls3->test();
class test4
{
private function judgeIntNullable(?int $num):int
{
if ($num === null) {
echo "値はnullです。デフォルト値の0を使用します。<br>";
$num = 0;
}
return $num;
}
function test(?int $num)
{
$this->judgeIntNullable($num);
}
function judgeIntNull(int $num):int
{
if ($num === null) {
echo "値はnullです。デフォルト値の0を使用します。<br>";
$num = 0;
}
return $num;
}
}
$cls4 = new test4();
echo $cls4->test(null);
echo $cls4->judgeIntNull(null);
class test4
{
private function judgeIntNullable(?int $num):int
{
if ($num === null) {
echo "Value is null. Using default value 0. <br>";
$num = 0;
}
return $num;
}
function test(?int $num)
{
$this->judgeIntNullable($num);
}
function judgeIntNull(int $num):int
{
if ($num === null) {
echo "Value is null. Using default value 0. <br>";
$num = 0;
}
return $num;
}
}
$cls4 = new test4();
echo $cls4->test(null);
echo $cls4->judgeIntNull(null);
| 文字列 | (int)でキャストした結果 | 説明 |
| "123" | 123 | 全て数値なのでそのまま変換されます。 |
| "123abc" | 123 | "123"までが数値として認識され、aで停止します。 |
| "12.3" | 12 | 小数点も数値の一部とみなされますが、(int)キャストなので小数点以下は切り捨てられます。 |
| "abc123" | 0 | 先頭が数値ではないため、結果は0になります。 |
| "0" | 0 | 0に変換されます。 |
| "" | 0 | 空文字列は0に変換されます。 |
class test5
{
function test($value)
{
if($value === 100)
{
echo "data is num.<br>";
}
else
{
echo "data is string.<br>";
}
}
}
$cls5 = new test5();
$cls5->test("100");
$cls5->test("100"+0);
class test5
{
function test($value)
{
if($value === 100)
{
echo "data is num.<br>";
}
else
{
echo "data is string.<br>";
}
}
}
$cls5 = new test5();
$cls5->test("100");
$cls5->test("100"+0);
| 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |