Archive for the ‘PHP’ Category

  1. Hello, PHP World!

    Posted on 2月 8th, 2012 by cx20

    PHP

    PHP は Web サーバー上で動作するサーバーサイドスクリプト言語である。
    CGI 版およびモジュール版(Apache や IIS の拡張モジュールとして動作)が存在する。
    PHP の類似技術としては、ASP、ASP.NET、Java Servlet、JSP(JavaServer Pages) 等がある。

    ソースコード(CGI 版)

    #!/usr/bin/php
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, PHP World!"; ?></p>
      </body>
    </html>

    ソースコード(モジュール版)

    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, PHP World!"; ?></p>
      </body>
    </html>

    実行方法(CGI 版)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.php
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.php

    実行方法(モジュール版)

    1. Web 公開フォルダ に配置
    2. ブラウザで表示
       http://localhost/doc/hello.php

    実行結果

    Hello, PHP World!
  2. Hello, CGI World!

    Posted on 2月 7th, 2012 by cx20

    CGI

    CGI(Common Gateway Interface)は、Web サーバー上でプログラムを動作させる為の仕様の一つである。
    言語としては、C言語 や Perl が主に使われるが、他の言語でも HTTP ヘッダ “Content-Type: text/html¥n¥n” を出力することで、CGI プログラムとして動作させることが可能である。
    CGI 以外の実行方式としてはモジュール形式(Apache や IIS の拡張モジュールとして動作)や FastCGI 形式がある。

    ソースコード(C言語)

    #include <stdio.h>
     
    int main( int argc, char* argv[] )
    {
        printf( "Content-type: text/htmlnn" );
        printf( "<html>n" );
        printf( "  <head>n" );
        printf( "    <title>Hello, World</title>n" );
        printf( "  </head>n" );
        printf( "  <body>n" );
        printf( "    <p>Hello, CGI World!</p>n" );
        printf( "  </body>n" );
        printf( "</html>n" );
        return 0;
    }

    ソースコード(Perl)

    #!/usr/bin/perl
    print "Content-Type: text/htmlnn";
    print "<html>n";
    print "  <head>n";
    print "    <title>Hello, World!</title>n";
    print "  </head>n";
    print "  <body>n";
    print "    <p>Hello, CGI World!</p>n";
    print "  </body>n";
    print "</html>n";

    ソースコード(PHP)

    #!/usr/bin/php
    <html>
      <head>
        <title>Hello, World!</title>
      </head>
      <body>
        <p><?php echo "Hello, CGI World!"; ?></p>
      </body>
    </html>

    実行方法(C言語)

    1. コンパイル
       $ cc -o hello.cgi hello.c
    2. CGI用フォルダ(cgi-bin等)に配置
    3. 実行権限の付与
       $ chmod +x hello.cgi
    4. ブラウザで表示
       http://localhost/cgi-bin/hello.cgi

    実行方法(Perl)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.pl
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.pl

    実行方法(PHP)

    1. CGI用フォルダ(cgi-bin等)に配置
    2. 実行権限の付与
       $ chmod +x hello.php
    3. ブラウザで表示
       http://localhost/cgi-bin/hello.php

    実行結果

    Hello, CGI World!
  3. Hello, Phalanger World!

    Posted on 1月 24th, 2012 by cx20

    Phalanger

    Phalanger は .NET 環境向けの PHP コンパイラである。名前は「PHP language compiler」の略。
    PHP の構文が使える他、.NET Framework のライブラリが使用できる。
    C# 同様に、UNIX 環境向けの .NET Framework 互換プロジェクト「Mono」により他の OS でも動作できるようになってきている。

    ソースコード

    <?php
    echo "Hello, Phalanger World!\n";
    ?>

    ソースコード(.NET ライブラリを使用した場合)

    <?php
    SystemConsole::WriteLine("Hello, Phalanger World!");
    ?>

    コンパイル方法

    C:¥> phpc hello.php

    コンパイル方法(.NET Framework を使用する場合)

    C:¥> phpc /lang:clr /r:mscorlib hello.php

    実行結果

    Hello, Phalanger World!
  4. Hello, PHP(CLI) World!

    Posted on 12月 19th, 2011 by cx20

    PHP

    PHP は Web アプリケーションのサーバーサイドに特化したスクリプト言語である。
    名前の由来は「Personal Home Page」から。現在は「PHP is Hypertext Pre-processer」の略とされている。
    PHP の実行方式としては、サーバーモジュール方式(mod_php, ISAPI)、CGI 方式、CLI 方式などがある。
    CLI 方式の場合は HTTP ヘッダ “Content-Type: text/html¥n¥n” が出力されず通常のコマンドラインアプリケーションとしての動作をする。

    ソースコード

    #!/usr/bin/php
    <?php
    echo "Hello, PHP World!n";
    ?>

    実行方法(スクリプトファイルを指定して実行)

    $ php hello.php

    実行方法(実行権限を付与して実行)

    $ chmod +x hello.php
    $ ./hello.php

    実行結果(CLI版として実行)

    Hello, PHP World!

    実行結果(CGI版として実行)

    X-Powered-By: PHP/5.3.6
    Content-type: text/html
     
    Hello, PHP World!