PHP Installation and IDE
Introduction to PHP
Hello and welcome to learncomputer.com’s PHP tutorial! You’re probably here because you want to learn PHP, which means you probably have a fairly good idea of what it is. We’ll skip the long, drawn-out introduction to the language and give it to you in a nutshell:
PHP is a server-side scripting language that can produce dynamic Web pages- some pretty complex ones, in fact. PHP allows you to do everything from receive input and pass it on as variables to creating, manipulating, and using a complex relational database to run extremely large and complicated websites like iStockPhoto or TechCrunch. The sky’s figuratively the limit with PHP, and in these tutorials we’re going to teach you how to harness this power for your own purposes and get you writing a website that uses PHP and relational databases in a real-world scenario.
We have to walk before we can run, though, and first things first: you need to install PHP on your system and get a way to work on it.
NOTE: This tutorial series assumes you are familiar and comfortable with using HTML and CSS. If you are not, please take a look at other guides for these technologies and make sure you are comfortable using them before you begin this guide!
Fortunately, installing PHP has become quite simple since PHP has become a part of most mainstream repositories, and PHP is often included in the package managers of the respective distributions. If you prefer to install from the command line, follow these instructions.
For apt-based distributions, you can simply type:sudo apt-get install php5 php5-common php5-cli php-gd php-mysql apache2
For yum-based distributions, you can type:sudo yum install php php-gd php-mysql httpd
And the package manager will take care of the rest. Please note at this point that you’re not going to be able to do a whole lot useful without a web server to test your PHP scripts on; the instructions above also install the Apache web server on your machine (“apache2” in the apt example and “httpd” in the yum example), which we’ll be using due to PHP’s inherent Web nature.
You’ll notice that we’re installing packages called “php-gd” and “php-mysql” now. These are called modules, and they extend PHP’s functionality. We’ll be using these in later chapters, so it’s easiest if we install them now. If you use apt’s or yum’s search functions, you may notice that PHP has a great deal of modules available. They all add different functionality, and we’ll explore some of them in later chapters, but for now we’ll stick with the basic ones.
Installing PHP on Windows
Installing PHP on Windows is as simple as downloading the PHP Windows installer from http://ww.php.org and following the instructions. Many Windows machines, however, do not come with web servers or MySQL installed. You can find a bundled installation at http://www.wampserver.com/en/ (WAMP stands for Windows Apache Mysql PHP), which will install the latest versions of Apache, MySQL, and PHP on your computer. If you choose not to use the WAMP installer, you can also install Apache and MySQL separately by downloading them from http://httpd.apache.org/ and http://www.mysql.com/ respectively.
NOTE: PHP is functional with many web servers. We’re using Apache, but there are other web servers like IIS or Lighttpd. Be aware that all the examples in the tutorials will be based on Apache and MySQL: if you choose to use a different web server, some of the examples may not work correctly or have different results.
Testing Your PHP Installation
Once your package managers or installers have finished installing PHP, we’re going to check that PHP is installed on your system currently. Open up your favorite plain text word processor (this means Notepad for Windows users, not Microsoft Word) and paste the following code block into the file:
<?php //show us all the info about our PHP installation phpinfo(); ?>
That’s probably not going to make too much sense to you yet- that’s alright! That’s an example of PHP code in action, and we’ll be covering that very soon. For now, just copy and paste that and save it in your web server’s HTML root directory as “phpinfo.php” (and pat yourself on the back- you’ve created your first PHP script!).
The HTML root directory can vary with your installation; the default installation directory is /var/www on most Linux distributions and, if you installed the WAMP all-in-one package above, the default Windows directory is C:\wamp\www (if you installed from Apache’s website, the default installation path is C:\Program Files\Apache Group\htdocs).
Once you’ve dropped your phpinfo.php file in your root HTML directory, open up your web browser of choice and go to “http://localhost/phpinfo.php”. Your screen should look something like this:
There’s lots of information there, and you don’t need to worry about it now. What that means is that your PHP + Apache install is working fine!
TROUBLESHOOTING: If the browser doesn’t connect and comes back with a server not found error, then you need to take a look at your Apache configuration: make sure Apache is running, and make sure it’s on the default port of 80.
Congratulations! Your PHP install is working, and you’re ready to get writing some PHP programs!
Installing The IDE
Before we get down into the nitty-gritty, however, we’re going to abandon Notepad or Gedit- though they served us well for our opening phpinfo page. We’re going to write our PHP in something called an IDE, or Integrated Development Environment: it’s an editor specifically designed to aid programmers in writing code. Though you can write PHP programs in a plain text editor like Notepad, IDEs have several advantages over plain-text editors, including syntax highlighting and auto-completion, that make a programmer’s life much easier. Trust us, once you start building huge web apps in PHP you’ll appreciate it!
PHP is a pretty popular language, and there are several powerful IDEs for it. Here’s a small list of some of the more popular free IDEs for PHP:
- Eclipse with PHP addon (PDT)
- Aptana Studio
- NetBeans with PHP plugin
This guide is going to use Eclipse with the PHP addon (PDT) for its example screenshots, but once you’ve completed the tutorial and have some coding experience under your belt we do encourage you to explore the IDEs and decide which one fits best with your coding style; many people prefer one to the other, and there’s really no “best” designation- there’s only which one you prefer to use!
Installing the IDE is quite simple on both platforms: Just head over to http://www.eclipse.org/pdt/downloads/ and grab the all-in-one version for your respective platform.
It’s important that you download from that particular page, as Eclipse has several different versions and plugins for several different programming languages. Also, as an Eclipse-specific note- Eclipse has no installer; you simply extract the program to a directory and it’s ready to launch. For our purposes, let’s extract it to your home directory (if you’re using Linux) or to C:\eclipse (if you’re in Windows). From there, go into that folder and open the Eclipse executable.
Congratulations! You’ve installed Eclipse!
Writing A PHP script with Eclipse
By now, you’re probably itching to write a PHP script (which you’ve technically already done) but we’re going to do it the Eclipse way by opening a project. Head over to Eclipse, and click File->New->PHP Project. You’ll get a screen that looks like this:
Name your project helloworld and leave everything else blank, and click finish. Once you’ve done that, click File->New->PHP file and name it helloworld.php and click Finish. A screen should pop up looking like this:
You’ll notice it’s been autoformatted with the
"<?php" tag, just as the phpinfo.php script did. You also might notice that it looks like the beginning of many other HTML tags, like
"<a>". That’s because it’s similar in concept: that tag tells the server to process everything inside that block as a PHP script, and return the results after they’ve been processed. In our case, we’re going to want to write this:
<html> <title> Hello World! </title> <body> <?php echo "Hello World!"; ?> </body> </html>
Go ahead and save that as helloworld.php, and move that into your web directory. You should see this:
Congratulations! You’ve built your first PHP page. You may have noticed in this example that we’ve put traditional HTML code into our page. The reason for this is because PHP works together with HTML; the HTML renders non-changing, or static content, in the page, while PHP renders dynamic, or changing, content to the page. You might argue that “Hello World” isn’t too dynamic, and you’re right: remember, though, we’re taking baby steps! PHP can do a heck of a lot more than just print “Hello World”, and we’ll take a look at that in our next segment, where we handle user input!