PHP Getting Ready
What do you need to write and run PHP code?
- Get to know the process of PHP requests and responses.
- PHP Development Web servers such as All-in-One Packages.
- Simple text-based editors or Feature-rich editors.
- Run your first PHP code.
PHP works together with other programming languages and technologies to present you with fully functional dynamically loaded content web pages. To fully understand how PHP loads dynamic content to users on the web page, you need to have some knowledge of HTML for inserting contents generated by PHP into web page elements and CSS for presentation and design. Refer to our tutorials about those two topics.
Loading and presenting a complete PHP-enabled dynamic page to the user works in a cycle of request and response.
When a user requests a web page in a browser, the browser sends a request to the server. The server processes the request and sends back a response to the user’s browser with the requested contents.
Here is an image showing how this request and response process works.
A brief explanation of the above image steps.
- The user enters the website address into the browser.
- The browser sends a request to the server across the local network or Internet.
- The web server analyses the request if it’s only HTML/CSS/JavaScript it processes it and returns a response with web contents to the user browser.
- If the request contains information that requires PHP to process it, forward the request to the PHP Interpreter (processor).
- The PHP interpreter processes the request and if information needs to be requested from a Database such as MySQL, it connects to the database.
- PHP runs some queries to get the required information from MySQL.
- The Output of executed code contains contents and information is returned to the Web server such as Apache.
- The web server returns a response to the user browser with the contents and information requested.
Warning
Unlike other programming languages such as Java and C++, PHP is more tolerant, but don’t expect PHP to be tolerant like JavaScript a Client-Side Scripting language.
What I mean by the above note is any syntax error in JavaScript will not prevent the whole page from rendering, except that particular syntax and related code will not work.
On the other hand, if PHP encounters a single fatal error in the whole script the web page will be prevented from rendering, in some cases the entire website will not work if a PHP process file is shared by the whole website.
More discussion on this will follow in our coming tutorials.
Web Development server’s installation and setup
You can simply write a code in HTML/CSS/JavaScript and run it in any browser installed on your OS, such as Windows. But if you need to use server-side languages such as PHP/ASP etc. you will need a remote or local development server equipped will the tools and software to execute the code.
Let’s look into some All-in-One Packages available for different Operating Systems.
WAMP, MAMP, XAMPP, or LAMP All-in-one Web servers
I’m going to briefly describe each of these web server packages. All of them come as pre-configured with a few settings, and you are good to go and run your PHP code.
In our upcoming articles and topics, I’m going to write a comprehensive comparison, installation, and setup of these web servers.
WAMP: An abbreviation for “Windows, Apache, MySQL, and PHP”, is free and an open resource Windows-based all-in-one bundled package. It can be installed on Windows and act as a fully functional webserver to create dynamic web pages using PHP and MySQL.
Visit https://www.wampserver.com/en/ for download and installation instructions.
XAMPP: Short for “X-operating system, Apache, MariaDB(MySQL), PHP, Perl” is a free and open-source web server package. As clear from its abbreviation it is an all-in-one bundled package, which has additional support for Perl and instead of MySQL DBMS (Database management system) it uses MariaDB(MySQL) DBMS. It has cross-platform support for Windows, Mac, and Linux installation.
Visit https://www.apachefriends.org/ for download and installation instructions.
LAMP: An abbreviation for “Linux, Apache, MySQL, and PHP”, is a Linux-based web server package. You will need Linux OS knowledge to install and set up the LAMP web server.
Visit the LAMP Server website for download and installation instructions.
MAMP: Stands for “Mac, Apache, MySQL, and PHP” is a MAC-based web server. It also comes as MAMP Pro, a commercial package with some additional functionalities.
Visit the MAMP Server Website for download and installation instructions.
Editor and IDE Software to write PHP code
Although you can write your PHP code using OS default plain-text editors, it will take less time and effort to use one of the feature-rich editors.
Feature-rich editors come with smart tools such as syntax highlighting, auto-completions facilities, and colour differentiation between tags, brackets, syntax, and attributes.
Visit HTML Getting Started to see a list of code/text editors.
Assuming you have managed to install one of the web servers and your choice code editor, go ahead and write and run a simple PHP code embedded into HTML as given below.
<!DOCTYPE html> <html> <body> <?php $language = "PHP"; $website = "W3Scoop"; $item1 = 10; $item2 = 2; ?> <?php echo "<h2>Welcome to " . $website . "</h2>"; ?> <p><?php echo "You will learn " . $language ; ?> here.</p> What day is today? It is <?php echo strftime("%A",time()); ?> today<br> What month is this? It is <?php echo strftime("%B",time()); ?><br> What date is today? It is <?php echo date("d/m/Y"); ?><br> <p>Your shopping items totals: <?php echo ($item1 + $item2); ?></p> </body> </html>
Output of the above example:
Example Output
Welcome to W3Scoop
You will learn PHP here.
What day is today? It is Thursday todayWhat month is this? It is November
What date is today? It is 21/11/2024
Your shopping items totals: 12
Note:
I’m working on an online IDE for PHP. I’m currently working on its security issues, which is going to be ready soon. Until then, you can choose from one of the above software for practice, also current example output will be displayed with every PHP code.