PHP is now the ubiquitous language of the web. Netcraft says that over 200M websites use PHP. Given the affordability of PHP web hosting, this is unlikely to change anytime in the near future. PHP projects such as WordPress, Drupal, Joomla, and others all have extensive installation bases, giving PHP an even stronger presence on the web.
While most languages evolve, none has done so to the degree that PHP has. PHP, formerly known as Personal Home Page, started as a series of bindings to C functions and, in version 3, changed to something more akin to its current version known as PHP: Hypertext Preprocessor. Some of this evolution will become somewhat painfully obvious as you become more familiar with the language. Despite this, if you are learning PHP as your first server side language, you have made a good choice.
If you have a PHP development environment already, skip the remainder of this paragraph. Otherwise, I recommend EasyPHP. EasyPHP is a package for Windows which provides, Apache, MySQL, and PHP – everything needed to starting building web applications. The installer can be downloaded here. I would recommend simply choosing all the defaults.
Most language introductions start with a `Hello World!’ example. In deference to those who came before us, we’ll do the same. This is “Hello World” in PHP:
Lets take the entire example and create a file called `hello.php’ in the our web directory. For those of you who installed EasyPHP and accepted the defaults, this is `C:Program FilesEasyPHP 2.0b1www‘.
If you are using Windows, you don’t have a good built-in editor. I recommend downloading and installing EditPad Lite. I have used EditPad Pro for quite some time and love it. The file in your editor should look like this:
After you have created hello.php file, in your browser, go to `http://localhost/hello.php‘ where you should see something like this:
Congratulations! Let’s start learning PHP. `<?php’ is known as an `open’ tag which tells PHP to start interpreting everything thereafter as PHP instructions. Another such tag exists, `<?’ when you have PHP’s short_tags configuration option on. It is recommended you refrain from using short tags.
The next statement is `print(“Hello World!n”);’. print is a PHP function which given an input, display’s said input on the screen. The last statement is `?>’ which simply tells PHP to stop processing.
If everything is working, you are done! You have successfully created your first PHP Web Application. If your having trouble running the example, I have below a list of common problems and solutions:
Error: `Page not found’, `Page Load Error’ or `cannot display the webpage’:
Either the address you typed in the browser is not correct or your web server is not started. If you are using EasyPHP right click on the black E in your system tray and click either start or restart. If you do not have a black E, go to Start -> Programs -> EasyPHP -> EasyPHP.
Error: `PHP Parse error: syntax error, unexpected $end in /home/noland/hello-world.php on line 4′:
The end quote is missing from `”Hello World!n”‘.
Error: `PHP Parse error: syntax error, unexpected T_STRING in /home/noland/hello-world.php on line 2′:
The start quote is missing from `”Hello World!n”‘.
Error: `PHP Notice: Use of undefined constant php – assumed ‘php’ in /home/noland/hello-world.php on line 3′:
Your end tag should be `?>’ not `php?>’.
Error: `PHP Parse error: syntax error, unexpected ‘?’ in /home/noland/hello-world.php on line 3′:
There is a space in your end tag. Use `?>’ not `? >`.
Error: `PHP Fatal error: Call to undefined function prnt() in /home/noland/hello-world.php on line 2′:
You missed spelled the print function.
If you have gotten this far, the toughest leg of your PHP journey is over. If you’ve have trouble, don’t fear, there is a large volume of knowledge out there and plenty more PHP tutorials to help. Regardless, there is much more to learn! Please stay tuned for more web development tutorials.