0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

PHPの=>と->の違い

Last updated at Posted at 2021-11-07

PHPに最近入門したのですが、JSを既に知っているとPHPの=>->に大変混乱させられます。アロー関数では全くないんですよね。。(Javaでも=>は同じような意味なのに...)

###=>は配列にアクセスするときに使う

index.php
$arrSamp = array(
    0 => 'Ringo',
    1 => 'Mikan',
    2 => 'Ichigo',
    3 => 'Kiwi'
);

print_r($arrSamp);

//表示結果
//Array
//(
//    [0] => Ringo
//    [1] => Mikan
//    [2] => Ichigo
//    [3] => Kiwi
//)

//Json Objectにも
echo json_encode(["error" => ["type" => "server_error", "message"]]);

//結果
//{"error":{"type":"server_error","0":"message"}}

###->はオブジェクトのメソッドやプロパティへアクセスするときに使う

Sample.php

class Sample {
        function hoge() {
           echo 'hoge';
        }
}
index.php

$Hoge = new Sample();
$Hoge->hoge(); //hoge

JSならSample.hoge()で呼び出すところですが、これが->になるんですね。めちゃくちゃ紛らわしいです。結構忘れるので備忘録。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?