Created
August 6, 2016 15:27
-
-
Save Restoration/44c0c8649e96bb79b5b007723ee61bef to your computer and use it in GitHub Desktop.
PHPのキャスト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE> | |
| <html lang="ja"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| </head> | |
| <body> | |
| <?php | |
| /* | |
| 型キャスト | |
| 変数の型を変換する方法 | |
| int型やstring型などの型を関数または型キャストを使って変換してきます。 | |
| */ | |
| //変数に整数を代入 | |
| $a = 123; | |
| //変数に文字列を入力する | |
| $b = '321'; | |
| echo '変数$aの型:' . gettype($a) . '<br>'; | |
| echo '変数$bの型:' . gettype($b) . '<br>'; | |
| //関数を使い型を変換します | |
| $a = strval($a); | |
| $b = intval($b); | |
| echo '変数$aの型:'.gettype($a). '<br>'; | |
| echo '変数$bの型:'.gettype($b).'<br>'; | |
| //型キャストで型を変換します | |
| $a = (int) $a; | |
| $b = (string)$b; | |
| echo '変数$aの型'. gettype($a) .'<br>'; | |
| echo '変数$bの型'. gettype($b) .'<br>'; | |
| ?> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment