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 5 years have passed since last update.

PHP覚書

Posted at

この記事はずっと下書きのまま放置していましたが、下書き整理のためそのまま投稿しています。

設定確認

ちょっと確認するぐらいなら php -i | grep で。

動作確認

ちょっと確認するぐらいなら php -r "echo 'hoge', PHP_EOL"; とかで。
PHP_EOL は環境に合わせた改行コードの定数。その他→定義済みの定数
1行でやりづらいなら php -a で。ファイルの実行は php hoge.php。ファイルを作成する際は <?php を最初に。

配列操作

要素確認

in_array

// in_array は指定の配列の値に対して、配列内に存在すればtrueを返す
php > var_dump(in_array(1, [1, 2, 3]));
php shell code:1:
bool(true)

php > var_dump(in_array(0, [1, 2, 3]));
php shell code:1:
bool(false)

php > var_dump(in_array(1, [1 => 10, 2 => 20, 3 => 30]));
php shell code:1:
bool(false)

php > var_dump(in_array(10, [1 => 10, 2 => 20, 3 => 30]));
php shell code:1:
bool(true)

array_key_exists

// array_key_exists は指定の配列のキーに対して、配列内に存在すればtrueを返す
php > var_dump(array_key_exists(1, [1 => 10, 2 => 20, 3 => 30]));
php shell code:1:
bool(true)

php > var_dump(array_key_exists(10, [1 => 10, 2 => 20, 3 => 30]));
php shell code:1:
bool(false)

追加・上書き

// + では追加はされるが、上書きはされない
php > var_dump(['a' => 1, 'b' => 2] + ['a' => 10, 'c' => 3]);
php shell code:1:
array(3) {
  'a' =>
  int(1)
  'b' =>
  int(2)
  'c' =>
  int(3)
}

// array_mergeなら、追加かつ上書きとなる
php > var_dump(array_merge(['a' => 1, 'b' => 2], ['a' => 10, 'c' => 3]));
php shell code:1:
array(3) {
  'a' =>
  int(10)
  'b' =>
  int(2)
  'c' =>
  int(3)
}
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?