字串是一連串的字元,前後以引號包住。 單引號和雙引號都支援。
<?php
$where = "Spain";
$what = 'rain';
在雙引號字串中,如果引用了作用域內的變數,該變數的值會被插入字串中。
<?php
$answer = 42;
$expanded = "The answer to life, the universe, and everything is $answer";
以往 PHP 的字串函式只支援 ASCII 字元,不支援現代的 Unicode 編碼。 每個 ASCII 字元佔 1 個位元組,而 Unicode 字元的長度可能是 1 到 4 個位元組。 如果需要安全地處理 Unicode 字串,請參考這些歷史函式的多位元組版本。
<?php
$byte_length = strlen('😃'); // => 4
$string_length = mb_strlen('😃'); // => 1
在這個練習中,你將幫助高中情侶在社群媒體上表達愛意,用他們的姓名縮寫產生一個 ASCII 愛心:
****** ******
** ** ** **
** ** ** **
** * **
** **
** J. K. + M. B. **
** **
** **
** **
** **
** **
** **
***
*
實作 HighSchoolSweetheart::firstLetter 函式。
它應該接受一個名字,並回傳它的第一個字母。
它應該清掉名字中任何多餘的空白。
<?php
$sweetheart = new HighSchoolSweetheart();
$sweetheart->firstLetter("Jane");
# => "J"
實作 HighSchoolSweetheart::initial 函式。
它應該接受一個名字,回傳它的第一個字母,轉成大寫並在後面加上一個點。
請務必重複使用你在上一個步驟中定義的 HighSchoolSweetheart::first_letter。
<?php
$sweetheart = new HighSchoolSweetheart();
$sweetheart->initial("jane");
# => "J."
實作 HighSchoolSweetheart::initials 函式。
它應該接受一個全名(由名字和姓氏組成,中間以空格分隔),並回傳姓名縮寫。
請務必重複使用你在上一個步驟中定義的 HighSchoolSweetheart::initial。
<?php
$sweetheart = new HighSchoolSweetheart();
$sweetheart->initials("Jane Doe");
# => "J. D."
實作 HighSchoolSweetheart::pair 函式。
它應該接受兩個全名,並回傳放在 ASCII 愛心裡的姓名縮寫。
請務必重複使用你在上一個步驟中定義的 HighSchoolSweetheart::initials。
<?php
$sweetheart = new HighSchoolSweetheart();
$sweetheart->pair("Blake Miller", "Riley Lewis")
# => """
# ****** ******
# ** ** ** **
# ** ** ** **
# ** * **
# ** **
# ** B. M. + R. L. **
# ** **
# ** **
# ** **
# ** **
# ** **
# ** **
# ***
# *
# """