Getting Started With PHP
Getting Started With PHP
Setting Up Your PHP Environment
If you’re looking to get started or are diving deeper into web programming, in particular designing web applications, you most likely at some point will be using PHP. If you are unsure of what exactly PHP is, and what exactly its used for, you can find more information on our article “Getting Started With PHP: An Overview and What It’s Used For“. With a general Idea of what it is, we’re ready to get started coding right? Well, not exactly. Even if we are able to write functioning PHP code, if we write a .php file, save it to our computer, and try to open it, it’s going to open in a text editor. Obviously, this isn’t what we want. We want to see our code get processed in a web browser. Therefore, first, before we start coding, we will need to set up a PHP environment to properly implement and test our code. What we will need to do this is: PHP itself, a web server, and a database. In this article we will touch briefly on what all of these components are, how they work together, and then finally the easiest way to set up the environment in order to use PHP for web programming. If you already know, or don’t care about the specifics you can just jump ahead to setting up the environment.
We won’t go too deep into it, but simply put, PHP is the code that is being complied/interpreted on your server. When we open a web page, it contacts the server that the web page content is held on, that server processes the PHP code, and relays it back to our browser. Think of it as a book. The server is the paper that the words are printed on, but the PHP is the actual text. Most servers that you use, including the one we will be using, already have PHP set up on them using a ‘direct modal interface (SAPI)’, so there is nothing we need to do to get the server to interact with the PHP. If this is not the case or you are interested in learning more about how the interaction between PHP and the server works you can find more here, but for the purpose of this article we will be assuming that the server you are using is already set up.
The Web Server
Say someone tries to visit our website. Their browser will make a request to our server using HTTP and the server will either respond with the web content, or an error message if it is unable to do so. Furthermore, while the server’s primary function is to serve content, it is also able to receive and store data from the clients. This is especially relevant to us because one of PHP’s primary uses is to both relay stored information from the server to the client, and relay information from the client and store it on the web server inside a database.
If we are trying to develop a site, or just test some code, it doesn’t exactly make sense to pay for a hosted server or have our site public. Therefore, what we do is set up a local web server on our computer. This server will store all our files and style sheets, and we will call our local web server directly when we open our web browser. This way the interaction is completely local, while still simulating a live web environment.
Databases are collections of information organised in a way that can be easily accessed, managed, and updated. They store data in the form of tables making it easier to find relevant information. One of PHP’s most valuable functions is to interact with the databases stored on web servers. A database isn’t exactly needed in order to set up our PHP environment, but it wouldn’t be complete without one. There are quite a few different database management systems out there but the most popular is probably MySQL, and that is what we will be using. When it comes to installing the different database systems on the server they each have their own unique steps, but the good news is that for our case, since we are using MySQL, there are PHP environment packages that will take care of this for us.
Setting Up The Environment
Now that we know a little about the different parts of web environments, let’s go ahead and set one up. The different parts can all be set up separately, however, there are pre-existing PHP environments that are available that already have everything we need. Since this article is about the easiest way to set up our web environment, we are going to use one of these, in particular XAMPP. Why reinvent the wheel after all.
XAMPP comes with the APACHE web server and MySQL database management system, and can be found here for both Linux and Windows systems. The installation for Windows is pretty foolproof, and instructions for Linux can be found here. During the set up, since we are just getting started, let’s go ahead and stick with the default settings. They will have everything we need.
Once installed, we will find a XAMPP folder within our local drive, so, let’s go ahead and open that folder. Inside, we are going to find a lot of different files and folders. We are going to ignore all of these except two folders, ‘htdocs’ (where we will save all of our web projects) and ‘phpMyAdmin’ (where we will set up the username and password for our server). Before we start any of our web projects let’s set up the security on our web server. So, go ahead and open the ‘phpMyAdmin’ folder and open the file ‘config.inc.php’ in a text editor. The opened file should look like this…
There is where we can change our login information so not just anyone has access to our server. We are going to leave our user as “root”, go ahead and change the password to whatever you want your password to be, and change ‘AllowNoPassword’ to false. Now, when we try to access our server in the next step we will be prompted to login.
In order for us to access our server we will need to open a web browser. So go ahead and open a browser and in the top bar enter “localhost”. We should come to a screen like this…
From here go to the phpMyAdmin tab. It will prompt you to log in. Enter the credentials that you created in the ‘config.inc.php’ file and you should come to a screen that looks like this…
This is your database. From here you can create tables and manage data that your PHP can then access. The only thing left to do is to create a web project and open it in our browser.
In order to do this, we are going to open our ‘htdocs’ folder. inside this folder is where we create all of our web projects. So, let’s go ahead and create a folder and name it ‘myProject’. inside this folder we are going to create a file and call it ‘index.php’. Inside this file we can now add our PHP code. To keep it simple we are going to add some very basic code, so go ahead and inside our file, between our php brackets, let’s write a hello world statement.
<?PHP echo “hello world”; ?>
and save it.
Now we are finally ready to test our code. Open up another web browser and in the top bar type ‘localhost/myProject/index.php’. This should give us…
There we go! We have now successfully created a PHP web environment and tested some code. While the code was simple, the same process will be used to create all our projects. All we need to do is save them in our ‘htdocs’ folder (which represents the localhost), and then search the path of the file we want from there. Best of luck, and feel free to check out some of our other “Get Started with PHP” articles such as how to set up a local WordPress environment, and how to include HTML inside your PHP files.