PHP: Hypertext Preprocessor was developed by Rasmus Lerdorf as a web tool to keep track of who was actually looking at his resume. Its first version was released in 1994 and since then, it was developed by the open-source community (visit php.net). PHP is one of the most popular server-side scripting language being used by web developers all around the world. If you have ambitions of becoming a web developer, PHP is a must for you and will allow you to create dynamic web pages based on external input. It is also important to note that PHP is an object-oriented programming language, and O.O.P. is one of the most important fundamental concepts every computer science student learns at the university level.
In the context of web development, communication occurs between several parties over the HTTP: Hypertext Transfer Protocol which is the client-server TCP/IP protocol used on the World-Wide Web to facilitate the exchange of HTML documents (Note that HTTP conventionally uses port 80).
Web pages : consists of text and other objects, written in HTML: Hypertext Markup Language.
Server : responsible for sending (i.e. serving) web pages in response to client requests.
Client : requests web pages from servers, receives them, and displays to the user. (Note that in most cases, the web browser acts as the client)
User : uses the client in order to surf the web and perform online tasks.
In both client and server side environments, there is code which runs on each machine. In client-side programming, we write programs to run on the Client, whereas in server-side programming, we write programs to run on the Server.
Client-side Vs. Server-side
- Runs scripts on the client (usually the web browser)
- The source code is transferred from the web server to the end user’s computer over the internet, but runs directly on the browser
- Processing occurs on the end user’s computer
- Interacts with temporary storage and local storage (cookies, local storage) and performs tasks dynamically on the web page
- Runs scripts on the web server
- A user’s request is fulfilled by running scripts directly on the web server and generate dynamic HTML pages which will then be sent to the client browser
- Processing occurs on the server computer
- Provide interactive web sites that interface database or other data resources stored on the server
- Most-used languages: PHP, Python, ASP.Net or nearly any language which has the capability to perform application-level web services (Java, C++, C#)
Tuning the Gear
It is time to get a very fresh PHP install on our machine. Although one can simply start client-side scripting on a simple code editor like Sublime Text or NotePad++, to run your server-side scripts, you need a server. If you already have a web hosting account, then you can run your scripts on top of it. Instead of this fancy option, you can still use your own computer as a server by installing a web-server software like XAMPP or WAMPP. For this tutorial, we are using XAMPP, a free and open-source cross-platform web server solution stack package developed by Apache Friends, consisting mainly of the Apache HTTP Server, MariaDB database, and interpreters for scripts written in the PHP and Perl programming languages. FYI, XAMPP stands for Cross-platform (X) , Apache (A) , MySQL (M) , PHP (P) , and Perl (P). Using this simple and light-weight Apache distribution, you can easily use your machine as a local web server for testing and deployment purposes.
Go to https://www.apachefriends.org/index.html using your web browser and click on the download link for XAMPP installer. Run the downloaded .exe file. For PHP programming, you only need Apache, MySQL, and phpMyAdmin. But install all the components from the setup window because you may need other components in the future. When your installation is complete, open the XAMPP Control Panel and Start the Apache and MySQL components. To verify the Apache and MySQL install, clicl on Admin buttons. If your browser successfully goes to XAMPP Welcome Page* (http://localhost/dashboard/) and phpMyAdmin* (http://localhost/phpmyadmin/) respectively, you are all set to go to the next phase.
Note that the default ports used by Apache Module is Port 80 and Port 443 and the default port used by MySQL Module is Port 3306. If these ports are accessed by other processes running on the machine, you have to either disable those processes or change the ports in XAMPP configuration. By clicking on Config button of each module, you can find and change the ports used by the module.
If both Apache and MySQL Modules are starting and running smoothly, you can skip the Common Problems section and directly go to the HelloWorld.php section at the bottom of this tutorial.
“Fun part begins at the end of your comfort zone”
You must be lucky if all your XAMPP components work fine at the first place. But you must be luckier if your XAMPP components show you errors on the log, because that is where you start learning the underlying concepts in these developer tools. Here are the nature of two most common problems XAMPP shows us.
Port 80 in use by “C:\Program Files (x86)\Skype\Phone\Skype.exe” with PID _!
Port 443 in use by “C:\Program Files (x86)\Skype\Phone\Skype.exe” with PID _!
Port 80 in use by Unable to open process with PID 4!
Apache WILL NOT start without the configured ports free!
You need to uninstall/disable/reconfigure the blocking applicationor reconfigure Apache and the Control Panel to listen on a different port
On XAMPP Control Panel, Stop Apache Module, click on Config button, then select Apache (httpd.conf). Find (Ctrl+F) 80 on the file. Replace all 80s with a random value you like (8080 looks good) and save the file.
Now click on Config button, then select Apache (httpd-ssl.conf). Find (Ctrl+F) 443 on the file. Replace all 443s with a random value you like (4433 looks good) and save the file.
Start the Apache Module again.
On XAMPP Control Panel, Stop Apache Module. Open Task Manager and go to Details tab. Find the process with the PID mentioned on the XAMPP error log. Select the relevant process carefully and click on End Task button. Start the Apache Module again.
Solution 3 (For Skype):
On XAMPP Control Panel, Stop Apache Module. Open the Skype desktop application, go to Tools, then select Options (Ctrl+,). Click on Advanced tab, then select Connection. Uncheck the selection: Use port 80 and 443 for additional incoming connections. Restart Skype and Start the Apache Module again.
Port 3306 in use by “C:\Program Files\MySQL\MySQL Server _._\bin\mysqld.exe”!
This occurs when you have installed two versions of MySQL and the other install has already acquired the port 3306 before the MySQL install coming with XAMPP. Changing the port is the easiest solution here.
First Stop the XAMPP server if it is already running. Click on Config button, then select my.ini. Find (Ctrl+F) 3306 on the file. Replace all 3306s with a random value you like (3307 looks good) and save the file. Start the MySQL Module again.
NOTE: When you type http://localhost/ on your browser’s address bar, it uses the port 80 by default. But if you make any changes to the default ports of Apache Module, make sure you adjust your localhost’s address accordingly. For an example, if you change your port 80 to port 8080, you will have to use http://localhost:8080/ all the time, so that your browser knows Apache is now using port 8080, not the default port.
NOTE: Sometimes above port configurations on files are not enough. You have to do something extra to get things right. So if you get the same error even after the port configuration, first Stop Apache Module. Click on config (The one in the right-hand side with the wrench icon) > Service and Port Settings > Apache > Change the “Main Port” and “SSL Port” for different values (e.g 8080 and 4433 look good) Click Save. Start the Apache Module again.
The main directory for all WWW documents is \xampp\htdocs. If you put a file “HelloWorld.html” in this directory, you can access it with the URI “http://localhost/HelloWorld.html”.
To test PHP, save the following testscript in \xampp\htdocs directory as “HelloWorld.php” and use “http://localhost/HelloWorld.php” on your browser.
Congratulation! You have successfully run the first PHP file on your machine. Happy coding folks!