Create a hello world script
If you have no experience with PHP (or any programming language for that matter) then this article is for you! In this article I’ll provide you with the resources and knowledge necessary to run your first PHP “Hello World” script.
THINGS YOU'LL NEED
- 1.Active internet connection
- 2.Microsoft Windows XP or better
OVERVIEW OF TASKS
Before we can start writing a PHP script, we need to transform your computer into a server that is capable of running PHP scripts. To do so, all we need is a program called XAMPP (mentioned under Great Software on the right). After we install and verify that XAMPP is working, we’ll make our first PHP “Hello World” script. If you find that something doesn’t make sense along the way, post a comment and I’ll do my best to help you out.
The XAMPP installer is about 60MB at the time of writing this article (10 September 2011) but is worth every kilobyte.
After downloading, start the installation process. Take note of the directory that it is installed in (we’ll be saving our PHP files there later).
There is no need to change any of the configuration settings, so just keep hitting next until XAMPP is fully installed.
If you use Skype, you’ll need to exit the application while running XAMPP. For some reason Skype likes to hog port 80 which is necessary for Apache to work (unless you change the configuration).
Open the XAMPP Control Panel application and click the Start button next to Apache
If your control panel doesn’t show “Running” next to Apache with a green background, read the next section entitled Troubleshooting Apache. If you do see “Running” next to Apache, you can skip the next section.
I assume you’re reading this section because Apache won’t start, so let’s see if we can fix that.
First things first, disable your firewall (if you don’t know how, search for instructions on Google.com for your specific firewall application)
If that doesn’t fix the problem, then we’ll try changing the port Apache binds to.
Open up the directory that you installed XAMPP to (most likely “C:\xampp”). Look for a folder named apache and then open it. Once there, open a folder named conf. This is where the configuration for Apache is located (httpd.conf). When you click Start on the XAMPP Control Panel, this is the file that is loaded and tells Apache how to run. So, open the file named httpd.conf with Notepad++ (Listed under Great Software on the right).
Search for “Listen 80” (it’s on line 47 for me). Change “Listen 80” to “Listen 8080”.
Then search for “ServerName localhost:80” (line 176) and change it to “ServerName localhost:8080”
Save the file and go back to your XAMPP control panel. Try clicking the Start button next to Apache and we should be in business. If not, I recommend you head over to XAMPP and read through some of the support documentation.
Congratulations! You’ve made it past the installation and configuration process – now we get to move on to the fun stuff!
Open up your favourite browser and navigate to http://localhost/
If you had to change the port Apache binds to in the Troubleshooting Apache section, then you’ll need to type in http://localhost:8080/.
You should see a page that says XAMPP and has various language options. If you like you can click through and see what XAMPP has to offer, but that isn’t the purpose of this article; we’re going to write our first PHP script.
FILES AND DIRECTORIES
Open the folder “c:\xampp\htdocs\” and create a new directory called “test”. This is the directory that we will be saving our PHP scripts to. You’ll need to navigate to http://localhost/test/ (again, if you had to switch the port that Apache binds to then add :8080 to the end of localhost) to access the contents of the directory.
MAKING A PHP SCRIPT
Open up Notepad++ and proceed to save the current document as helloworld.php in c:\xampp\htdocs\test\. To run the file, open your browser to http://localhost/test/helloworld.php.
Here is what our script needs to look like to output “Hello World” to the browser.
Copy and paste the code into your helloworld.php script, save, and go to http://localhost/test/helloworld.php
WHAT DOES IT MEAN?
The echo command does exactly what you’d expect; it echoes whatever you tell it to straight to the browser. Note that Hello World is in quotes and is followed by a semi-colon. The semi-colon says that the current command needs to be terminated and to move on to the next line.
Now that you have a basic understanding of the hello world script, let’s throw in a variable.
Delete everything in helloworld.php and paste the new code in, save, and go to http://localhost/test/helloworld.php.
Note that the script is nearly identical to the first variable-less PHP script we wrote with a couple exceptions. $string is a PHP variable and is capable of storing nearly anything. In this case, we are storing ‘Hello World’ in $string. Later on, rather than writing ‘Hello World’ after the echo, we simply say $string.
And that’s about it for the basic introduction to PHP! If everything worked right, you should now have an environment in which you can execute PHP files! You should also be able to start and end a PHP script.