What is PHP & How to Set-up a PHP Web Server? – Part – I

Welcome to the first part of the “PHP for Beginners” course. In this course, my aim is to make you understand almost everything you need to learn to get started with PHP. It is mainly targeted for the absolute beginners who are just going to start learning PHP and also for the intermediate learners who have a little knowledge of development with PHP.

I would try to explain everything in-depth and also in a very simple and lucid language, so that anyone can easily understand what’s really going on and enhance their learning curve. I won’t go into any complex aspects of PHP like OOP (Object Oriented Programming) approach with PHP as they are absolutely beyond the scope of this “beginners” course but will surely be covered in future courses I do.

Before starting this course, I would like to throw some light on what PHP actually is and how it works? Without a proper understanding of this, you will never be able to feel the real essence of PHP. I would also discuss the step-by-step process of setting up your local server which you will need to develop PHP applications faster.

What is PHP?

php-logo

(Image Source: softaculous.com)

To explain in the simplest way, PHP is a server-side, cross platform scripting language which can be used for creating dynamic websites quickly. Originally PHP used to stand for Personal Home Page (PHP), but later got changed to PHP: Hypertext Preprocessor (PHP) following a recursive acronym.

PHP was originally developed by Rasmus Lerdorf and later continued as an open source development. This cross-platform language is a lot influenced by programming languages like C, C++, Java and have got a lot of similar looking syntax. So, if you have a little knowledge about any of these languages, then learning PHP would be even easier for you.

The popularity of PHP is evident from the fact that it is here with us for over 19 years and have helped us create super-powerful webpages like Facebook.com, WikiPedia.com, WordPress.com, Flickr.com, etc.

Now let us learn some of the most important aspects of PHP

i)    Server-side Language

PHP is a server-side language which simply means that all the processing work needed to run a PHP file is done entirely on the server and not on the client’s local machine. Now if you try to run a PHP file in your computer and you do not have PHP, then you will never be able to run it. You always need a server with PHP installed to run PHP files.

ii)    Scripting language

PHP is also known as a “scripting language” as PHP programs are not compiled and can be written using any simple text editor like Notepad. But the real work is done behind the curtain, by the interpreter sitting in the server (with PHP installed) to run the .php files.

You may have heard many developers calling their PHP programs as “scripts”, and now you know the reason behind it.

iii)    Cross-platform language

PHP is an excellent cross-platform language or you can also call it as a platform-independent language. PHP can run on most platforms like Windows, Linux, BSD, Mac OSX, Solaris – to just name a few.

PHP is also compatible with all servers used today like the IIS, Apache, etc. which gives a great flexibility to the developers using a variety of servers and platforms.

How PHP Works?

how-php-works

PHP can be used to create really powerful websites with dynamic functionalities. PHP scripts can be used to send/receive emails, read/write information to files, store/retrieve information from databases and much more.

Now from the previous discussion, it should be fairly understandable how the PHP files actually work (server-side, remember), but let us understand in a better and simpler way.

Suppose, you visit a particular webpage in your web browser by typing its URL. Now the page you see has been built with HTML and you can view the entire source code of that HTML file in the web browser itself. But HTML is WYSIWYG (What You See Is What You Get) and lacks the ability to create dynamic web pages. Nothing can change in an HTML page until and unless you hand-code it to change.

Now suppose that HTML page has got a form for user registration where it takes information like username, password, email, etc in text fields. All these information’s taken are sent to a PHP file (embedded into the HTML file) for further processing. You can never view the source code of a PHP file (just like you did with an HTML file), as it is run completely on the server and not on the client’s local machine.

Now you may want to check if those information’s supplied by the users are valid using some logical PHP codes and if the user registration is successful then those information’s can be stored on a database for future use and an email can be sent for activation.

So, now the working of a PHP file should be a bit clear to you. Don’t worry, if it’s still not as you will learn better while developing actual PHP files soon.

Editor’s Note:

php-manual

(Image Source: php.net)

One of the best things about PHP, is that it is has got a very extensive documentation which you can refer anytime in their official website – PHP Manual.

I would personally recommend you to head-over to this website and have a brief overview of it because while developing PHP applications we will need to refer to this manual quite often, so it’s better to get acquainted with it right from the beginning.

Suppose you want to know more about a certain function like “phpinfo()” then you can just head-over to the online manual and type “phpinfo” in the search box and you will be vested with everything you need to know about that particular function.

How to Set-up a PHP Server?

Now, I am sure that you are excited enough to start writing your first PHP file. But as you have learned that PHP is a server-side language so you need to have a server (with PHP installed) to run PHP files.

Now you have two options before you:

i)    You can purchase a web host and upload and run PHP files there. Almost all free and paid web hosts have got PHP installed and allows you to run PHP scripts with ease. But if you want quick development, then this should never be your choice, as uploading PHP files every time you make small changes can be a real time killer.

ii)    The second option is to turn your local machine into a mini web server and test PHP files locally with ease. Yes, it perfectly possible to create a mini web server inside your local computer using various software’s like XAMPP (for Windows, Linux, Mac OS X, Solaris), WAMP (for Windows), MAMP (for Mac), etc.

Now, I am going to show you how you can set up your mini web server using XAMPP. You can obviously go for the other options (that I have mentioned before) depending upon your platform and choice of interest. But I prefer to use XAMPP and will show you the installation process using it. But don’t worry, as the installation and the setup process using the other ones are also quite similar.

xampp-logo

(Image Source: opencodez.com)

 

Step 1: Download and install XAMPP

Simply visit this site and download the setup for your operating system. The installation process is very simple and is absolutely same as installing other general software’s in your OS, so I am not going to go deep into it.

Step 2: Start the Apache and MySQL in XAMPP

xampp-control-panel

Once you have completed the installation of XAMPP, double click on the XAMPP Control Panel icon and open the control panel. It’s a very simple and straight-forward control panel which lets you start/stop various services that comes bundled with XAMPP.

In our purpose, we need the Apache service running for all PHP applications, so just click the Start button to start the service. There is another service which we will need in some PHP applications in the later part of the course which needs to access the databases – MySQL (I will explain about MySQL later in this series). So, it’s better to start the MySQL service too.

Once you have ensured that both Apache and MySQL are running perfectly, open your favorite web browser and type http://localhost and you will be taken over to the XAMPP Splash screen.

 xampp-splash-screen

Congratulations, you have successfully set-up your new mini web server in your local computer.

You can also click on the “phpMyAdmin” link in the left sidebar and check the service which comes bundled with XAMPP and is used to control the MySQL databases (which will be discussed later).

What’s next?

Now in this part of the series, you have learned what PHP actually is and how it works on various webpages. You have also learned about various software’s and the step-by-step process which can be used to turn your computer into a mini web server.

So, now you are officially ready to start developing PHP applications on your local computer. In the next part of the series we will create our first PHP file, run it in our web server and also learn about various interesting and important things about PHP.

STAY UP TO DATE

Sign up today to stay informed with industry news & trends