Skip to main content

「PHP」と「C/C++/C#」の違い

Scripted vs compiled languagesインタープリタ言語とコンパイル言語

Compiled Languagesコンパイル言語

C, C、C++ and C# are what is known as "Compiled Languages"、C#は「コンパイル言語」と呼ばれる。まずテキストファイル(. They start with a text file (c/.c / cpp/.cpp / .cs). This file is then converted into an executable file (.exe in Windows), that can be run.cs)を作る。そのファイルを、実行できるファイル(Windowsなら.exe)に変換する。

image.png

This process of converting a text file into an executable file is called "build" or "compilation". During this process, text instructions are converted into binary code that the computer can understand. Moreover, code is often "optimized" to run faster.このテキストファイルを実行ファイルに変換する作業を「ビルド」または「コンパイル」と呼ぶ。この過程で、文字で書かれた命令が、コンピュータが理解できる2進数(バイナリ)のコードに変換される。さらに、もっと速く動くように、コードが「最適化」されることも多い。

For example, a very short for loop could be "unraveled" into repeated instructions to to avoid going back in code.例えば、とても短いforループでも、コードの中を何度も戻らないように、同じ命令を繰り返す形に「展開」されることがある(下の表を参照)。

Original Codeオリジナル Unraveled code展開
for (int i = 0; i < 4; i++)
{
    printf("Hello world!\n");
}

printf("Hello world!\n");
printf("Hello world!\n");
printf("Hello world!\n");
printf("Hello world!\n");

The optimized code avoids a comparison (最適化されたコードでは、比較(i < 4), gets rid of the variable i, since it never gets used, and avoids jumping back at the end of each for loop, which makes the code faster.4)が要らなくなり、使われない変数iも消え、forループの終わりで前に戻る動作もなくなる。だからコードが速く動く。

For this reason, compiled languages are fast when compared to...このような理由で、コンパイル言語は次に紹介する言語と比べて動作が速い。

Interpreted Languagesインタープリタ言語

Interpreted languages include things like Lua, Python, and Javascript. These languages are not compiled into binary but use an "interpreter". An interpreter is a special software that converts each line of code, one by one, into binary at runtime.インタープリタ言語には、Lua、Python、JavaScriptなどがある。これらの言語はバイナリにコンパイルされず、「インタープリタ」というものを使う。インタープリタとは、コードを1行ずつ、実行するその瞬間にバイナリへ変換する、特別なソフトのことである。

The major advantage of Interpreted languages is that they do not need compilation. You can make minor changes to the code without having to rebuild your executable file. The disadvantage, however, is because it only executes one line at the time, it cannot be optimized (the interpreter does not know what is coming later down the code, so it cannot optimize it)image.png

Another advantage of Interpreted languages is that they can run almost anywhere (Windows, Linux, MacOS) as is, as long as there is an interpreter for that platform.インタープリタ言語の大きな利点は、コンパイルが要らないことである。実行ファイルを作り直さなくても、コードを少し変えるだけで反映できる。ただし欠点もある。1行ずつしか実行しないので、最適化ができない(インタープリタは、この先にどんなコードが来るか分からないので、最適化のしようがない)。


インタープリタ言語のもう一つの利点は、その環境用のインタープリタさえあれば、Windowsでも、Linuxでも、MacOSでも、そのままほぼどこでも動くことである。

PHP

PHPHPはインタープリタ言語である。つまり、コンパイルは不要である。PHPのインタープリタは、たいていWebサーバーのプラグインとして動く。今回の授業では、XAMPP is aApache(Webサーバー)とPHPが最初から入っているので、そのまま使うことができる。

interpreted

拡張子が language..php Thisのページを開くと、Apache means that no compilation is required. The PHP interpreter usually runs as a web server plugin. In our use case, XAMPP already comes with Apache (Web Server) + PHP already installed, so we can use it right away.

When we call a special webpage that ends with .php, Apache runs the PHP interpreter, converts the result to HTML and shows it to us as a normal webpage.インタープリタを動かし、その結果をHTMLに変換して、普通のWebページとして表示してくれる。

image.png

image.png

PHP runs once (no state saved)PHPは1回だけ動く(状態は保存されない)

PHP was created as a way to create dynamic webpages. This means, web pages in which the content can change. For example, each time you refresh X (Twitter), you get a new list of posts, as people post new content. Even though you're reloading the same page, the content is changing dynamically.PHPは、動的なWebページを作るために生まれた言語である。動的なWebページとは、内容が変わっていくページのことである。例えば、X(旧Twitter)を更新するたびに、みんなが新しい投稿をするので、新しい投稿一覧が表示される。同じページを読み込んでいるのに、中身は動的に変わっているわけである。

Because the way it was designed, PHP runs only once per page. Remember that web connections are stateless (connect -> get content -> disconnect). Because of this, there is no point in keeping a PHP program running after the page has been returned to the user. PHPはそのような仕組みで作られているので、1ページにつき1回しか動かない。Web接続は「状態を持たない(ステートレス)」ことを思い出してほしい(接続する→内容を受け取る→切断する、という流れである)。だから、ページをユーザーに返した後もPHPのプログラムを動かし続ける意味がないのである。

This is a very important distinctions to a regular program. We are used to having variables live throughout the life of the program, but because PHP ends when the page has been served, no data remains in between page reloads. Each time we load a page, a brand new program starts, with its own variables and internal state.これは、普通のプログラムとの大きな違いである。私たちは、プログラムが動いている間ずっと変数が生き続けるのに慣れている。しかしPHPは、ページを返した時点で終了してしまうので、ページを再読み込みするたびにデータは何も残らない。ページを読み込むたびに、まったく新しいプログラムが、新しい変数と内部状態を持って始まるのである。

image.png

In order to keep data between calls, there are mechanisms that can be used (such as cookies or sessions). These mechanisms, however, are outside of the scope of this class. Those students that are interested, may refer to the textbook in page 113~119呼び出しの間でデータを保持したい場合は、いくつかの仕組み(クッキーやセッションなど)を使うことができる。ただし、これらの仕組みは今回の授業の範囲外である。興味のある人は、教科書の113~119ページを見てください。

We will need, however, to pass variables between calls. We will see how can we use GET and POST data to keep the server aware of some data between PHP script executions when we develop our Unity + PHP game.とはいえ、呼び出しの間で変数を渡す方法は必要になる。UnityとPHPを使ったゲームを作るときに、GETデータとPOSTデータを使って、PHPスクリプトの実行と実行の間でサーバーにデータを覚えさせる方法を学んでいく。