The Language Understood
PHP is an interpreted language meaning it is read and parsed by the binary PHP libraries then interpreted into binary that the operating system can understand. This differs from C, C++, or Java where the code is compiled into binary for the operating system to execute. There are clear benefits to developing with an interpreted language:
- Allows simple creation and editing in a variety of text editors.
- Edit and running of code is fast.
On the other hand there are a few disadvantages:
- Usually run quite slowly.
- Limited access to low level and speed optimization code.
- Limited commands to run detailed operations on graphics.
Additionally it has been said that interpreted languages, in general, have additional benefits like:
- Easy to learn and use.
- Minimum programming knowledge or experience.
- Allows complex tasks to be performed in relatively few steps.
- Allows the addition of dynamic and interactive activities to web pages.
However, I can not stand behind these statements as I find Java easy to learn, understood with relatively minimal knowledge or experience, it completes complex tasks in relatively few steps, and dynamic and interactive web pages can be created with it.
In the Beginning was the Tag
When PHP parses a file, it looks for opening and closing tags, which are <?php and ?> which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows PHP to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser.
PHP also allows for short open tag <? (which is discouraged since it is only available if enabled using the short_open_tag php.ini configuration file directive, or if PHP was configured with the --enable-short-tags option).
For our very first exercise let create a file that we will execute a web browser. To create the file go to the web root in your workspace and create a new file called index.php. Open this file in the text editor of your choice. On Windows systems you can use Notepad, or on a Unix system I prefer Vi/Vim. Totally up to you to utilize the weapon of your choice. Open the file and on line one write:
echo "Hello World!";
Go to your browser and enter http://localhost in the address bar. did you get the text echo "Hello World!"; on a white background page, or an error message? You did! Great! This is because the code did not execute due to the fact that we did not add the opening and closing tags for the PHP interpreted to recognize the code. If you see the text then the file was sent to the broswer as a plain text file.
Let's add the rest of the pieces of code that we need. Open the file again, and edit the file to match what you see below.
<?php echo "Hello World!"; ?>
Save the file and refresh your browser. You should see a clear difference. The page should now display Hello World!. If that is what you see then congratulations. You have just executed your first PHP script.