PHP: リテラル

PHP: リテラル

September 2, 2020
PHP
PHP

スカラー型のリテラル #

<?php
var_dump(
  // bool値のリテラル、case-insensitive
  TRUE,
  True,
  true,
  truE, // これでもいい
  FALSE,
  False,
  false,
);
bool(true)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
bool(false)
<?php
var_dump(
  // int値のリテラル、8進数、16進数、2進数もある
  100,
  0123,
  0xBEEF,
  0b00101101,
  123_456_789, // php 7.4.0 から桁区切りにアンダースコアが使える
  0x1F_2B_3C_4D,
);
int(100)
int(83)
int(48879)
int(45)
int(123456789)
int(522927181)
<?php
var_dump(
  // flaot値のリテラル
  1.234,
  1.2e3,
  7E-10,
  1_234.567, // php 7.4.0 から桁区切りにアンダースコアが使える
);
float(1.234)
float(1200)
float(7.0E-10)
float(1234.567)
<?php
var_dump(
  // int値で内部表現的にオーバーフローするとfloat値になる
  9223372036854775807,
  9223372036854775808,
);
int(9223372036854775807)
float(9.2233720368548E+18)
<?php
// 文字列型のリテラル
$val = "hi";
$heredoc = <<<EOT
this is heredoc
variable $val is interporated
EOT;
$nowdoc = <<<'EOD'
this is nowdoc
variable $val is not interporated
EOD;
var_dump(
  // string値のリテラル
  'single quotation',
  "double quotation",
  'single quotation with $val',
  "double quotation with $val",
  $heredoc,
  $nowdoc,
);
string(16) "single quotation"
string(16) "double quotation"
string(26) "single quotation with $val"
string(24) "double quotation with hi"
string(43) "this is heredoc
variable hi is interporated"
string(48) "this is nowdoc
variable $val is not interporated"

複合型のリテラル #

リテラルで書けるのは配列ぐらいか。

<?php
// 言語組み込み関数のarray()、厳密にはリテラルではないか
$a1 = array(1, 2, 3);

// [] 記法
$a2 = [1, 2, 3];

// 連想配列
$a3 = array('a' => 1, 'b' => 2, 'c' => 3);

// [] 記法の連想配列
$a4 = ['a' => 1, 'b' => 2, 'c' => 3];

var_dump([
  'a1' => $a1,
  'a2' => $a2,
  'a3' => $a3,
  'a4' => $a4,
]);

var_dump([
  '要素のアクセスは[]記法' => $a1[0]
]);
array(4) {
  ["a1"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["a2"]=>
  array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
  }
  ["a3"]=>
  array(3) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
    ["c"]=>
    int(3)
  }
  ["a4"]=>
  array(3) {
    ["a"]=>
    int(1)
    ["b"]=>
    int(2)
    ["c"]=>
    int(3)
  }
}
array(1) {
  ["要素のアクセスは[]記法"]=>
  int(1)
}

特殊型のリテラル #

<?php
// NULL、case-insensitive
var_dump(
  NULL,
  Null,
  null,
  nuLl,
);
NULL
NULL
NULL
NULL

その他 #

厳密にはリテラルではないかもだが、直接値を記述しているという意味でまとめる。

コマンド出力 #

<?php
echo `date`;
Mon Sep  7 20:41:34 UTC 2020

無名関数 #

<?php
$fn1 = function($name) { return "hello {$name}\n"; };
$fn2 = fn($n) => "hello {$n}\n";

echo $fn1("john");
echo $fn2("mike");
hello john
hello mike

無名クラス #

<?php
class SomeClass {}
interface SomeInterface {}
trait SomeTrait {}

var_dump(
  new class {
  },
  new class(10) {
    private $num;
    function __construct($num) {
      $this->num = $num;
    }
  },
  new class() extends SomeClass implements SomeInterface {
    use SomeTrait;
    function __construct() {
      echo __CLASS__ . "\n";
      echo __METHOD__ . "\n";
    }
  },
);
class@anonymous/usr/src/myapp/a.php:15$2
class@anonymous/usr/src/myapp/a.php:15$2::__construct
object(class@anonymous)#1 (0) {
}
object(class@anonymous)#2 (1) {
  ["num":"class@anonymous":private]=>
  int(10)
}
object(class@anonymous)#3 (0) {
}