4
4

More than 5 years have passed since last update.

PHPとかいう糞言語について

Last updated at Posted at 2013-03-08

最初見た時、あいかわらず酷でーwww
とか思ったんだが

あれ?っと思って試したら予想通りだった

作者の意図するコードは↓こうなる

a.php
<?php

$array = array(1,2,3);

$ref &= $array[1];

$copy = $array;
$copy[0] = 'a';
$copy[1] = 'b';
$copy[2] = 'c';

foreach( $array as $v ) {
  print "${v}\n";
}

?>

実行結果

% php a.php 
1
2
3

修正前
$ref = &$array[1];
修正後
$ref &= $array[1];

  • 考察
    PHPの&は参照を生成する構文だった!

  • まとめ
    そんなの知るかー!

  • 追試

% php -r '$arr = array(1,2,3); $ref &= $arr[1]; var_dump($arr); var_dump($ref);' 
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
int(0)

あれ?

% php -r '$arr = array(1,2,3); $ref &= $arr[1]; $ref = "b"; var_dump($arr); var_dump($ref);'
array(3) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
}
string(1) "b"

あれあれ??

うん &=は意味ないかも…

4
4
3

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
4
4