Home> programming > PHP の 型変換

PHP の 型変換

  • June 13, 2006 11:35 PM
  • programming

<?php
//型キャストの実験
$testStr1 = "next365days";
echo $testStr1;//next365days
//自動キャストしてくれるのか
$testStr1 += 365;
echo $testStr1;//365...0+365って扱いらしい
//もう一回
$testStr1 = "next365days";
$testStr1 = (int) $testStr1;//今度は明示的に型変換
echo $testStr1;//0...やっぱりか
//数字から始まる場合は?
$testStr2 = "1024yen";
echo $testStr2;//1024yen
//自動的に型変換してくれるのか
$testStr2 += 1024;
echo $testStr2;//2048...いけた
//もう一回
$testStr2 = "1024yen";
$testStr2 = (string) $testStr2 + 1024;//わざわざ文字列キャスト後の計算もしてみる
echo $testStr2;//2048...やっぱり整数値へキャストされた...
?>

先頭に文字が含まれていなければ、自動的にキャストしてくれるみたい。
(今まで気にしてなかったのか!)

キャストできる型は、boolean, intege, float, string, array, object, resource, null。
resourceとかobjectとか使う機会あるのか...?

Trackbacks:0

TrackBack URL for this entry
https://w3neu.net/mt/mt-tb.cgi/63
Listed below are links to weblogs that reference
PHP の 型変換 from Sequentially Altered Days

Comments:0

Comment Form

Index of all entries

Home> programming > PHP の 型変換

Categories
Archives
Syndication

Return to page top