php學習筆記:可變變數、字串運算子和數組運算子

來源:互聯網
上載者:User
<?php
#php的可變變數
/*可變變數就是變數名可以動態設定和使用的變數。
一個可變變數擷取了一個普通變數的值作為這個可變變數的變數名。
因為普通變數的值是可變的,所以可變變數的變數名也是可變的。
*/
//可變變數適合在什麼場合使用呢?
$a = "hello";//定義一個普通變數
$$a = "world";//定義一個可變變數
echo "$a/n";//output:hello
echo "${$a}/n";//使用可變變數
//同echo "$hello/n";//output:world
echo "$hello/n";
?>

<?php
#php的字串運算子
//串連運算子(“.”)
$a="first";
$b=$a."==>second";//now $b is "first==>second"
echo "$b/n";

//串連賦值運算子(“.=”)
//the same to $a=$a."==>second"
$a.="==>second";//now &a is "first==>second"
echo "$a/n";

/*其實可以理解為就只有一種,即串連運算子
這裡的點(".")串連運算子和java語言中的字串串連符("+")是類似的。*/
?>

<?php
#php的數組運算子:+
/* PHP 僅有的一個數組運算子是 + 運算子。
它把右邊的數組附加到左邊的數組後,但是重複的索引值不會被覆蓋。
亦即,以左邊的數組為主導,若附加其上的(右邊的)數組中有與其key重複的部分將被忽略
*/
$a = array("a" => "apple", "b" => "banana");
$b = array("a" =>"pear", "b" => "strawberry", "c" => "cherry");
$a1=array("c"=>"a1_cherry","d"=>"a1=d");
$c = $a + $b;
var_dump($c);
/*output:
array(3) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
}
*/

$d = $a + $b+$a1;
var_dump($d);
/*output:
array(4) {
  ["a"]=>
  string(5) "apple"
  ["b"]=>
  string(6) "banana"
  ["c"]=>
  string(6) "cherry"
  ["d"]=>
  string(4) "a1=d"
}
*/
?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.