質問
配列のend
で以下パターンBができないのはなぜですか?
php
1<?php 2//パターンA 3$str = 'a,b'; 4$tmp = explode(',',$str); 5$end = end($tmp); 6echo $end;
php
1<?php 2//パターンB 3$str = 'a,b'; 4$end = end(explode(',',$str)); 5echo $end;
試したこと
だって、上のそれぞれのend
の中身を下記のように比較したらtrue
ですよね?
php
1<?php 2$str = 'a,b'; 3$tmp = explode(',',$str); 4$a = $tmp; //パターンAの中身 5$b = explode(',',$str); //パターンBの中身 6var_dump( $a === $b ); //true
ならend
の中身は同じ値ということですよね?
ではなぜパターンBはできないのですか??
まぁ、できなくはないのですが、
$end = @end(explode(',',$str));
これは Bad Practice なので、、
$end = array_slice(explode(',', $str), -1)[0];
とでもしてみてはどうでしょうか。

回答1件
あなたの回答
tips
プレビュー