LoginSignup
5
5

More than 5 years have passed since last update.

PHPメモ&tips

Last updated at Posted at 2016-01-26

PHPメモ&tips

組み込みサーバー

これは知らなかった。
下記コマンドでbuild in サーバが立ち上げられます。
PHPerの中では常識なのかな?知らなかった。

php -S localhost:3333

port番号はお好きに。
これでhttp://localhost:3333にアクセスすれば簡易的なWebサーバが立ち上がる。
試しに以下のようなindex.phpを置いてみる。

index.php
<?php
echo 'Hello Everybody!!';

そしてサーバーを立ち上げて

php -S localhost:9999

ブラウザでアクセス!!

スクリーンショット 2016-01-26 19.09.45.png

表示された。
artisan serveとかもこの仕組み流用してんのかな??

ちなみに5.4からみたいです。

詳しくは本家参照。

構文チェック

php -l ファイル名

これでPHPの構文チェックが走る。

仮に以下のtest.phpを作ってみる

test.php
<?php
$a = 123;
var b = 123;

んで

php -l test.php

とすると

$ php -l test.php 
PHP Parse error:  syntax error, unexpected 'var' (T_VAR) in test.php on line 3

Parse error: syntax error, unexpected 'var' (T_VAR) in test.php on line 3
Errors parsing test.php

構文エラーを検知し、知らせてくれる。

対話シェル(REPL)

php -a

これで対話的にphpの挙動を即座に確認できます。

$ php -a
Interactive shell

php > $hoge = 123;
php > print($hoge);
123

こんな感じ。PHPのAPI挙動を素早く調査したい時に便利ですね!いちいちブラウザ立ち上げて画面でvar_dumpも煩わしい。

array_pushのオーバヘッド

実際にオペコードとかで検証したわけじゃないので、一般論的なこととしてメモ。

$array = array();
for ($i = 0; $i < 100; $i++) {
     array_push($array, $i)
}

よりも

$array = array();
for ($i = 0; $i < 100; $i++) {
     $array[] = $i;
}

の方が早いらしい。今度ちゃんと検証してみよう。

とりあえず計測だけしてみた。

$array[]の場合.
php -a
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { $array[] = $i; } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00002秒

平均で0.00002秒

array_pushのケース.
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00003秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00008秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00003秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00003秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00004秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00004秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00003秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00005秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00003秒
php > $array = [];$time_start = microtime(true); for ($i = 0; $i < 100; $i++) { array_push($array,$i); } $timelimit = microtime(true) - $time_start; echo "処理時間:".sprintf('%0.5f',$timelimit)."秒";
処理時間:0.00005秒

平均で0.00004秒

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