First of all, you will need to have access to a web server that is set up to work with PHP. Don’t let this scare you. Typically, most of the web hosting companies today will offer PHP support, so you shouldn’t have a problem finding one. HostGator.com is an affordable solution, as is EagerHost.com. Both of these hosting companies fully support PHP.
Also, I want to make it clear that PHP is not a difficult language to learn, even if you have never tried to program before. In fact, if
you are completely new to programming, PHP is often the easiest way to get started.
In the world of the wide web, there are two general types of coding languages: “Server-side” and “Client-side”. This means that one type is run on the web server itself, and the other is run in your web browser.
PHP happens to be a server-side language. All of the processing is done on the web server itself, and the result is delivered to your web browser as HTML (which, by the way, is a Client-side language).
To begin, PHP code looks like this:
<?php ← opens your code
Additional code here
?> ← closes your code
In fact, PHP always begins with <?php and always ends with a ?> If your code is not contained within these tags, the PHP engine on the web server will display errors or ignore it all together.
Another important point to make is that pages that contain php must end in .php in order to work correctly. This means if you create an index page that contains php code, it must end in .php such as index.php, not index.html. If you use .html, the web server will once again ignore it. (There are a few exceptions to this but we will cover that in a later chapter).
You should also know that PHP can be embedded into HTML, and you don’t have to have raw PHP code in a PHP file, you can always switch between HTML and PHP, all on one page, as long as the page extension is .php (index.php, etc).
Here’s an example:
My First PHP Experience
echo ‘ My First PHP Experience’;
If you look closely at the simple script above, you will see that the script starts off as any HTML page would, with an open HTML tag, a head tag, and a body tag. None of this is PHP, it’s purely HTML, the language used to design web pages.
The PHP script starts where you see the line like this:
You can start to create a page in HTML as shown above and insert PHP script anywhere you like as long as you open it up with
Another important aspect of PHP is that each line must end with a semi-colon (;). If it doesn’t, your code might spit out errors.
Example: echo ‘ My First PHP Experience’; ← Note the semi-colon at the end of the line. This is very important! Without the semi-colon, this code would not work correctly.
As I mentioned above, in order to work with PHP you need a web server that allows it, which most do. The reason the web server needs to support PHP is because PHP code needs to be executed, and the PHP interpreter handles this.
Here’s an example of how this works:
- You load up your favourite browser and type in www.somesite.com
- The website, somesite.com receives your request to load the page.
- The web server checks to determine what kind of document it is that is required to load. It detects that it is a .php page (index.php). The web server calls on the PHP engine and transmits the contents of the index.php page.
- The PHP Engine loads the document and everything between the open and close tags ( Remember: <?php and ?> )and converts it into HTML output, so you can see it.
While this all sounds really complicated, it happens so quickly that all you see is the website load in your browser. The power behind it, however, is quite impressive.
Using the example above, if you were to load that page into your browser, you would not see the PHP code, all you would see is the output from the script.