Creating your first PHP program with XAMPP

Creating your first PHP program with XAMPP

We are now going to move on to server side scripting language, we are going to create a PHP application for the web. To begin this tutorial, you need a PHP server, either installed on your computer or perhaps a hosting service provider. For the purpose of this tutorial we are going to use XAMPP. XAMPP bundles all the PHP and MySQL server that you need to run the program we are about to code, so to begin head over to XAMPP download and install the application.

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.

Once XAMPP is installed open the program and start the Apache server, if all is well and you visit http://localhost/ you should see a typical yellow/orange background XAMPP screen, ok this means that we now have an Apache server that has the PHP module installed and all is well so let's write our program. Note: It might be the case that when you try to start the server the port 80 is not available and you cannot start the server, should this happen follow these steps:

  1. Open the file [XAMPP Installation Folder]/apache/conf/httpd.conf. typically c:/xampp/apache/conf/httpd.conf on a windows machine
  2. Now search for the string Listen 80 (I’m assuming that your XAMPP was using the port 80). This is the port number which XAMPP uses. Change this 80 to 8081 or to any other number which you prefer.
  3. Then search for the string ServerName and update the port number there also.
  4. Now save and restart XAMPP server
  5. You should now be able to access http://localhost:8080 :-)

XAMPP is a free and open source cross-platform web server solution stack package, consisting mainly of the Apache HTTP Server, MySQL database, and interpreters for scripts written in the PHP and Perl programming languages.

Now we are going to write some code, to start, create a file called myapp.php in the htdocs directory under XAMPP, for Windows this is typically c:\xampp\htdocs\myapp.php if you now visit http://localhost/myapp.php you shall see a blank screen that's because we haven't told our app what to do yet. Now you can open your favorite code editor "SciTE" and enter the following code:

<?php 
 echo "<h1>Welcome to MyApp</h1>";
?>

Now visit http://localhost/myapp.php again you will see that the PHP interpreters have now run your code on the fly, and a bold Welcome to MyApp text is displayed on the screen, this is neat because you don't have to write any compile code PHP is a very powerful scripting language, so you write your code "very loosely" and PHP module does the rest.

A scripting language or script language is a programming language that supports scripts, programs written for a special run-time environment that can interpret (rather than compile) and automate the execution of tasks that could alternatively be executed one-by-one by a human operator.

Let's gather some input for our app, say we want to display a parameter from the URL http://localhost/myapp.php?name=vusani Enter the following code in your myapp.php script file and save it:

<?php 
 echo "<h1>Welcome to MyApp {$_GET['name']}</h1>";
?>

Now run the program by visiting http://localhost/myapp.php?name=vusani, your app will now display the dynamic name that is fetched from the parameter list. Congratulations you have now experienced PHP and there's a whole lot more, play around and experiment with PHP code, also you can visit the extensive PHP documentation site to help you on your way ;-)