【PHP】変数を整数型に変換する関数「intval()」
今回はPHPで使用できる関数、「intval()」を使用してみます。「intval()」は変数の型を整数型に変換する関数です。
変数の型を整数型に変換する関数「intval()」
|
1 |
intval(整数型に変換したい変数); |
以下サンプルコードです。
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php //intval(整数型に変換したい変数); $test1 = "10"; $test2 = "10.123"; $test3 = "型"; $test4 = "+10"; $test5 = "-10"; $test6 = array(); $test7 = array(1 , 2 , 3); $test8 = array(10, 20 ,30); echo intval($test1)."<br>"; //実行結果:10 echo intval($test2)."<br>"; //実行結果:10 echo intval($test3)."<br>"; //実行結果:0 echo intval($test4)."<br>"; //実行結果:10 echo intval($test5)."<br>"; //実行結果:-10 echo intval($test6)."<br>"; //実行結果:0 echo intval($test7)."<br>"; //実行結果:1 echo intval($test8)."<br>"; //実行結果:1 ?> |