現在の言語: 日本語 |
string型 |
php型宣言 |
class test1
{
function testLineCode()
{
echo "(1) first row.\nsecond row.".PHP_EOL;
/*
(1) first row.
second row.
*/
echo "(2) this is \'apple\'.".PHP_EOL;//(2) this is \'apple\'.
}
function escapeString()
{
$data = "<script>console.log(\"hello world\");</script>";
echo htmlspecialchars($data, ENT_QUOTES, "UTF-8").PHP_EOL;//<script>console.log("hello world");</script>
}
function urlEncode()
{
echo urlencode("https://nekofes.github.io").PHP_EOL;//https%3A%2F%2Fnekofes.github.io
}
}
echo "<pre>";
$cls1 = new test1();
$cls1->testLineCode();
$cls1->escapeString();
$cls1->urlEncode();
echo "</pre>";
class test1
{
function testLineCode()
{
echo "(1) first row.\nsecond row.".PHP_EOL;
/*
(1) first row.
second row.
*/
echo "(2) this is \'apple\'.".PHP_EOL;//(2) this is \'apple\'.
}
function escapeString()
{
$data = "<script>console.log(\"hello world\");</script>";
echo htmlspecialchars($data, ENT_QUOTES, "UTF-8").PHP_EOL;//<script>console.log("hello world");</script>
}
function urlEncode()
{
echo urlencode("https://nekofes.github.io").PHP_EOL;//https%3A%2F%2Fnekofes.github.io
}
}
echo "<pre>";
$cls1 = new test1();
$cls1->testLineCode();
$cls1->escapeString();
$cls1->urlEncode();
echo "</pre>";| シーケンス | 説明 | 結果 | 使用例 |
| \" | ダブルクォーテーション | `" | echo "彼女は\"Hello\"と言った"; |
| \\ | バックスラッシュ自体 | \ | echo "C:\\Users\\"; |
| \n | 改行(LF: Line Feed) | 改行 | echo "1\n2"; |
| \r | キャリッジリターン(CR: Carriage Return) | 行頭に戻る | (環境依存の挙動、通常は\nを使用) |
| \t | 水平タブ | タブ文字 | echo "fruit:\tapple"; |
| \$ | ドル記号 | $ | echo "price is \$10."; |
| \[0-7]{1,3} | 8進数表現 | ASCII/バイナリ値 | echo "\101"; は "A" を出力 |
| \x[0-9A-Fa-f]{1,2} | 16進数表現 | ASCII/バイナリ値 | echo "\x41"; は "A" を出力 |
| \u{xxxxxx} | Unicodeコードポイント | Unicode文字 | echo "\u{3042}"; は "あ" を出力 (PHP 7以降) |
class test2
{
function castInt()
{
$str1 = "1.23";
$str2 = "0.5";
$str3 = "5\$";
$str4 = "5円";
$str5 = "num1";
$str6 = "hello";
echo "(1)".(int)$str1.PHP_EOL;//1
echo "(2)".(int)$str2.PHP_EOL;//0
echo "(3)".(int)$str3.PHP_EOL;//5
echo "(4)".(int)$str4.PHP_EOL;//5
echo "(5)".(int)$str5.PHP_EOL;//0
echo "(6)".(int)$str6.PHP_EOL;//0
}
function castFloatDouble()
{
$str1 = "1.23";
$str2 = "0.5";
$str3 = "100";
echo "(1)".(float)$str1.PHP_EOL;//123
echo "(2)".(float)$str2.PHP_EOL;//0.5
echo "(3)".(float)$str3.PHP_EOL;//100
}
function castBool()
{
$str1 = "";
$str2 = "0";
$str3 = "1";
$str4 = "00";
$str5 = "0.0";
$str6 = "1.0";
$str7 = "100";
$str8 = "hello";
echo "(1)".$this->boolResult((bool)$str1).PHP_EOL;//false
echo "(2)".$this->boolResult((bool)$str2).PHP_EOL;//false
echo "(3)".$this->boolResult((bool)$str3).PHP_EOL;//true
echo "(4)".$this->boolResult((bool)$str4).PHP_EOL;//true
echo "(5)".$this->boolResult((bool)$str5).PHP_EOL;//true
echo "(6)".$this->boolResult((bool)$str6).PHP_EOL;//true
echo "(7)".$this->boolResult((bool)$str7).PHP_EOL;//true
echo "(8)".$this->boolResult((bool)$str8).PHP_EOL;//true
}
private function boolResult($value):string
{
return $value === true ? "true" : "false";
}
function castArray()
{
$str = "test";
$ary = (array)$str;
$str = "a";
for($i=0; $i
string(1) "a"
}
*/
$index = 0;
foreach ($obj as $key => $value)
{
echo "index:$index プロパティ $key の値は $value です。\n";
++$index;
}
//index:0 プロパティ scalar の値は a です。
$len = mb_strlen($str);
for($i=0; $i<$len; ++$i)
{
echo "i:$i obj:".mb_substr($str, $i, 1).PHP_EOL;
}
//i:0 obj:a
//下記はエラーとなる例
/*
$str = "a";
$obj = (object)$str;
for($i=0; $icastArray()
#1 {main}
thrown in /Applications/MAMP/htdocs/testXXX.php on line lineNo
*/
}
function castNull()
{
$str = "test";
$str = null;
echo $str ?? "this is null.".PHP_EOL;//this is null.
$str = "test";
echo $str ?? "this is null.".PHP_EOL;//test
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->castInt();
$cls2->castFloatDouble();
$cls2->castBool();
$cls2->castArray();
$cls2->castObject();
$cls2->castNull();
echo "</pre>";
class test2
{
function castInt()
{
$str1 = "1.23";
$str2 = "0.5";
$str3 = "5\$";
$str4 = "5 yen";
$str5 = "num1";
$str6 = "hello";
echo "(1)".(int)$str1.PHP_EOL;//1
echo "(2)".(int)$str2.PHP_EOL;//0
echo "(3)".(int)$str3.PHP_EOL;//5
echo "(4)".(int)$str4.PHP_EOL;//5
echo "(5)".(int)$str5.PHP_EOL;//0
echo "(6)".(int)$str6.PHP_EOL;//0
}
function castFloatDouble()
{
$str1 = "1.23";
$str2 = "0.5";
$str3 = "100";
echo "(1)".(float)$str1.PHP_EOL;//123
echo "(2)".(float)$str2.PHP_EOL;//0.5
echo "(3)".(float)$str3.PHP_EOL;//100
}
function castBool()
{
$str1 = "";
$str2 = "0";
$str3 = "1";
$str4 = "00";
$str5 = "0.0";
$str6 = "1.0";
$str7 = "100";
$str8 = "hello";
echo "(1)".$this->boolResult((bool)$str1).PHP_EOL;//false
echo "(2)".$this->boolResult((bool)$str2).PHP_EOL;//false
echo "(3)".$this->boolResult((bool)$str3).PHP_EOL;//true
echo "(4)".$this->boolResult((bool)$str4).PHP_EOL;//true
echo "(5)".$this->boolResult((bool)$str5).PHP_EOL;//true
echo "(6)".$this->boolResult((bool)$str6).PHP_EOL;//true
echo "(7)".$this->boolResult((bool)$str7).PHP_EOL;//true
echo "(8)".$this->boolResult((bool)$str8).PHP_EOL;//true
}
private function boolResult($value):string
{
return $value === true ? "true" : "false";
}
function castArray()
{
$str = "test";
$ary = (array)$str;
$str = "a";
for($i=0; $i
string(1) "a"
}
*/
$index = 0;
foreach ($obj as $key => $value)
{
echo "index:$index property $key The value of is $value. \n";
++$index;
}
/index:0 The value of the property scalar is a.
$len = mb_strlen($str);
for($i=0; $i<$len; ++$i)
{
echo "i:$i obj:".mb_substr($str, $i, 1).PHP_EOL;
}
//i:0 obj:a
//The following is an example of an error
/*
$str = "a";
$obj = (object)$str;
for($i=0; $icastArray()
#1 {main}
thrown in /Applications/MAMP/htdocs/testXXX.php on line lineNo
*/
}
function castNull()
{
$str = "test";
$str = null;
echo $str ?? "this is null.".PHP_EOL;//this is null.
$str = "test";
echo $str ?? "this is null.".PHP_EOL;//test
}
}
echo "<pre>";
$cls2 = new test2();
$cls2->castInt();
$cls2->castFloatDouble();
$cls2->castBool();
$cls2->castArray();
$cls2->castObject();
$cls2->castNull();
echo "</pre>"; | 著作権情報 |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームページおよびアプリ等を利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |