LoginSignup
0
1

More than 3 years have passed since last update.

[PHP] ローカルサーバーの立てかた

Posted at

概要

ここまでphpをterminalで走らせていたが、実際の使用ではブラウザ上で処理、表示を行うケースが圧倒的に多いのでローカルサーバーの建て方をメモる。

プログラム

greet.php
<?php
  date_default_timezone_set('Asia/Tokyo');
  $now = date("Y/m/d H:i:s:");
  $now_hour = (int)date(G);

  function aisatsu($greet) {
    if ($now_hour >= 6 && $now_hour < 12) {
      $greet = 'おはようございます';
    } elseif ($now_hour >= 12 && $now_hour < 18) {
      $greet = 'こんにちは';
    } else {
      $greet = 'こんばんは';
    }
    return $greet;
  }

  $aisatsu = aisatsu($greet)
 ?>

<!DOCTYPE html>
<html lang="ja">
    <head>
        <meta charset="UTF-8">
        <title>タイトル</title>
    </head>
    <body>
        <p><?php print $aisatsu; ?></p>
    </body>
</html>
  date_default_timezone_set('Asia/Tokyo');

で取得した現在時刻に合わせて「おはよう」「こんにちは」「おやすみなさい」のどれか一つを表示するプログラム。

webサーバーを立てる

1. phpビルトインのローカルサーバーを起動する

実行したいphpファイルが入っているディレクトリまでterminalで移動し、下記を実行する

$ php -S localhost:8000

右端の数値は 8000 にしておく

2. ブラウザで表示する

検索バーに

http://localhost:8000/greet.php

と打つだけ

表示結果

こんばんは
0
1
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
1