Core PHP 15 Q/A 5 MCQs

Learning Objectives – after this chapter you will be able to:

  • Explain what open source software is and why it matters for web development.
  • Describe the history of the Open Source Initiative (OSI).
  • Explain the role of Apache, MySQL/MariaDB and PHP – the AMP stack – in a dynamic web site.
  • Trace how a browser request travels through Apache, PHP and the database and back.
Brief Introduction to PHP, Apache and MySQL

PHP, Apache and MySQL are all part of the open source group of software programs. The open source movement is a collaboration of some of the finest minds in computer programming. By allowing the open exchange of information, programmers from all over the world contribute to make a truly powerful and efficient piece of software available to everyone.

Through the contributions of many people to the publicly available source code, bugs get fixed, improvements are made, and a good software program becomes a great one over time.

ComponentRole in a web site
ApacheThe web server – receives requests from browsers and sends back pages.
MySQL / MariaDBThe database server – stores data in tables and answers SQL queries.
PHPThe server-side scripting language – runs on the server, talks to the database and builds the HTML that is sent to the browser.
A Brief History of Open Source Initiatives

The term open source was coined in 1998, after Netscape decided to publish the source code of its popular Navigator browser. This announcement prompted a small group of software developers, long-time supporters of the open source ideology, to formally create the Open Source Initiative (OSI) and the Open Source Definition.

  • Although the OSI ideology was first promoted in the hacker community, after the release of Navigator's source code programmers from all walks of life began to offer suggestions and fixes to improve the browser's performance.
  • Linux became the first operating system that could be considered open source (BSD, distributed from Berkeley in 1989, was a close runner-up), and many programs followed soon after.
  • Large software corporations began to offer versions of their programs that worked on Linux machines.
  • Any software that carries the OSI-approved licence can be considered open source, because it has passed the Open Source Definition test.
Why Open Source Rocks

Open source programs are popular with developers because:

  • They are free. Open source software is available to the general public. Developers volunteer their time to improve existing software and to create new programs. By definition, open source software cannot require any licensing or sales fee.
  • They are cross-platform and technology-neutral. Open source programs must not depend on "any individual technology or style of interface". As long as the software can run on more than one operating system, it meets the criterion – so it is usable by virtually everyone.
  • They must not restrict other software. If an open source program is distributed along with other programs, those programs may be open source or commercial. This gives developers maximum control and flexibility.
  • They embrace diversity. Open source programs cannot discriminate against any person, group or "field of endeavour". A program designed for medical use cannot be limited to that field if someone else wants to modify it for their own needs.

For the complete list of criteria a piece of software must meet to be called "open source", see the Open Source Definition on the OSI web site: opensource.org/osd.

How the Pieces of the AMP Module Work Together

Imagine that your dynamic web site is a fancy restaurant. Diners come to your place and each one wants something different and specific. They do not worry about how the food is prepared, as long as it looks and tastes delicious. Unlike a buffet, where everything is laid out and patrons simply choose from what is available, a good restaurant encourages patron–waiter interaction and complete customisation. Similarly, a web site should not be a static page with little interaction; it should be a dynamic site where the visitor chooses what he or she wants to see.

In this scenario the three components of the AMP module are:

ComponentRestaurant role
ApacheThe chef – a highly trained master of culinary arts. Whatever people ask for, she prepares quickly and without complaint. Apache, the HTTP server, parses the requested files and passes on the results.
PHPThe waiter – takes the request from the patron and carries it to the kitchen with specific instructions about how the meal should be prepared.
MySQLThe stockroom of ingredients – in this case, information (data).

When a patron (a web site visitor) sits down and orders a meal with specific requirements – a steak, well done – the waiter (PHP) takes that requirement back to the kitchen and passes it to the chef (Apache). The chef goes to the stockroom (MySQL) to fetch the ingredients (data), prepares the meal and presents the final dish to the patron exactly the way he or she ordered it.

1. Browser The visitor types http://localhost/shop/product.php?id=7 or submits a form.
↓
2. Apache Receives the HTTP request, sees the .php extension and hands the file to the PHP engine.
↓
3. PHP Executes the script; when it needs data it sends an SQL query to the database.
↓
4. MySQL / MariaDB Runs the query and returns the matching rows to PHP.
↓
5. PHP → Apache PHP builds the final HTML page and gives it back to Apache.
↓
6. Browser Apache sends the HTML to the browser, which displays it. The visitor never sees the PHP code.

Figure 1.1 – The life of one request in the AMP stack

You can install one, two or all three components of the AMP package, depending on your needs. If you are responsible for a company intranet or host your own web site, you will usually install all three. If your site is hosted by a third-party hosting company, these components are already installed on the server for you.

Install locally, test locally

Installing all three components on your own computer lets you develop and test your site in the comfort of your own workspace, without uploading to a server after every small change. Even after a lot of offline testing, always perform a complete test once your site is live, because the settings of your hosting company's server may differ from yours – and even a small difference can cause big headaches. The easiest way to install the complete AMP stack on one machine is XAMPP, described in Chapter 2.

Apache

Apache acts as your web server. Its main job is to parse any file requested by a browser and return the correct result according to the code in that file. The Apache HTTP Server is free, powerful and flexible, and can accomplish virtually any task that a webmaster requires. XAMPP 8.2.12 ships Apache 2.4.58.

Important features of the Apache HTTP Server include:

  • Password-protected pages for a multitude of users.
  • Customised error pages (for example, your own 404 Not Found page).
  • Content negotiation – serving the version of a document the browser can best accept.
  • Usage (access) and error logs in multiple, customisable formats.
  • Virtual hosting – many web sites (domains or IP addresses) served from one server.
  • Directory index directives (for example index.php, index.html).
  • URL aliasing and rewriting (mod_rewrite), with no fixed limit.

Apache can host a web site for the general public, a company-wide intranet, or simply a test copy of your pages before they are uploaded to a production server.

PHP

PHP is a server-side scripting language that allows your web site to be truly dynamic. PHP stands for PHP: Hypertext Preprocessor – a recursive acronym. Its flexibility and relatively small learning curve (especially for programmers who know C, Java or Perl) make it one of the most popular scripting languages on the web. Businesses and individuals everywhere use it, and its benefits clearly outweigh the costs – it is free and open source.

This book uses PHP 8 – the version shipped with current XAMPP releases. You can download PHP and read the complete function reference at www.php.net.

MySQL and MariaDB

Another open source favourite, MySQL is the database server that lets PHP and Apache work together to store, access and display data in a readable format in the browser. It is a Structured Query Language (SQL) server designed for heavy loads and complex queries. As a relational database system, it allows many tables to be joined together for maximum efficiency and speed.

MariaDB is a community-developed fork of MySQL, created by MySQL's original developers. It is a drop-in replacement – the same SQL, the same tools and the same PHP functions work with both. Since XAMPP 5.5.30/5.6.14 (2015) XAMPP ships MariaDB in place of MySQL, but the Control Panel still labels it "MySQL".

Popular features of MySQL/MariaDB:

  • Multiple CPUs usable through kernel threads.
  • Multi-platform operation (Windows, Linux, macOS …).
  • Numerous column types that cover virtually every type of data.
  • Group (aggregate) functions for calculations and sorting.
  • Commands that show information about databases quickly and clearly to the administrator.
  • Function names that do not clash with table or column names.
  • A password and user-privilege system for security.
  • Many indexes per table, supporting very large tables with millions of rows.
  • Error messages available in many languages.
Key Takeaways
  • Apache (web server), MySQL/MariaDB (database server) and PHP (server-side language) form the AMP stack.
  • All three are open source: free, cross-platform, non-restrictive and open to everyone.
  • The term "open source" was coined in 1998; the OSI maintains the Open Source Definition.
  • In a request, Apache receives it, PHP runs the script and queries the database, and the browser receives only HTML.
  • XAMPP ships MariaDB, a drop-in replacement for MySQL.
Question / Answer (15 questions)

Answer : AMP stands for Apache, MySQL and PHP – the web server, the database server and the server-side scripting language used together to build dynamic web sites. On Linux the stack is called LAMP, on Windows WAMP, and the cross-platform package is XAMPP.

Answer : Open source software is software whose source code is publicly available, so that anyone may use, study, modify and redistribute it according to its licence. Programmers all over the world contribute fixes and improvements, so good programs become better over time.

Answer : It was coined in 1998, after Netscape announced it would publish the source code of its Navigator browser. Long-time supporters of the idea then formed the Open Source Initiative (OSI) and wrote the Open Source Definition.

Answer : The OSI is the organisation that maintains the Open Source Definition and approves licences that meet it. Software released under an OSI-approved licence can be considered open source.

Answer : They are free; they are cross-platform and technology-neutral; they must not restrict other software distributed with them; and they embrace diversity – they cannot discriminate against any person, group or field of endeavour.

Answer : Linux is considered the first open source operating system, with BSD (distributed from Berkeley in 1989) as a close runner-up.

Answer : Apache is the chef who prepares whatever is requested, PHP is the waiter who carries the request and instructions, and MySQL is the stockroom that holds the ingredients – the data.

Answer : Apache is a web (HTTP) server. Its main job is to receive requests from browsers, parse the requested file and send the correct result back to the browser.

Answer : Password-protected pages, customised error pages, usage and error logs in customisable formats, virtual hosting of several sites on one server, directory index directives and URL rewriting (any four).

Answer : PHP stands for "PHP: Hypertext Preprocessor". It is recursive because the first word of the expansion is the acronym itself.

Answer : PHP is a server-side language. The script is executed on the web server and only the resulting HTML is sent to the browser, so visitors cannot see the PHP code.

Answer : MySQL is an open source relational database management system that uses Structured Query Language (SQL). It stores data in related tables and is designed for heavy loads and complex queries.

Answer : MariaDB is a community-developed fork of MySQL made by MySQL's original developers. It is a drop-in replacement – the same SQL and the same PHP functions work – and XAMPP has shipped MariaDB instead of MySQL since 2015.

Answer : A local installation lets you develop and test every change on your own computer without uploading files after each step. You should still test again on the live server because its settings may differ.

Answer : The browser sends an HTTP request to Apache; Apache passes the .php file to the PHP engine; PHP runs the script and sends an SQL query to MySQL/MariaDB; the database returns rows; PHP builds an HTML page; Apache sends that HTML back to the browser, which displays it.
MCQ Test : Check Your Understanding (5 questions)

Select an option and press Check Answer, or answer all questions and press Submit Test to see your score. Each answer comes with an explanation.

Q1. The term "open source" was coined in:

Show Answer

Correct Answer : C. 1998
The term was coined in 1998, after Netscape released the Navigator source code.

Q2. In the AMP restaurant analogy, PHP plays the role of the:

Show Answer

Correct Answer : B. Waiter
PHP is the waiter – it carries the visitor's request and instructions between the patron and the kitchen.

Q3. Which component of the AMP stack is the web server?

Show Answer

Correct Answer : C. Apache
Apache is the HTTP (web) server; MySQL/MariaDB is the database server and PHP is the scripting language.

Q4. PHP: Hypertext Preprocessor is an example of a:

Show Answer

Correct Answer : A. Recursive acronym
The acronym PHP appears inside its own expansion, so it is recursive.

Q5. Which database server does current XAMPP actually ship under the "MySQL" label?

Show Answer

Correct Answer : D. MariaDB
Since 2015 XAMPP has shipped MariaDB, a drop-in replacement for MySQL.

Bibliography/References

The notes on this page are the author’s own compilation from the e-book PHP: Basics to Advance. Every program was run on Apache with PHP 8 and MariaDB, and the output shown is the real result of that run. The works listed below were consulted and are cited for attribution and further reading. Product names and trademarks belong to their respective owners.

  1. Open Source Initiative. (n.d.). The Open Source Definition. https://opensource.org/osd
  2. The Apache Software Foundation. (n.d.). Apache HTTP Server Version 2.4 Documentation. https://httpd.apache.org/docs/2.4/
  3. The PHP Group. (n.d.). PHP Manual – What is PHP? https://www.php.net/manual/en/intro-whatis.php
  4. MariaDB Foundation. (n.d.). MariaDB Server Documentation. https://mariadb.com/kb/en/documentation/
  5. Wikipedia contributors. (n.d.). XAMPP. Wikipedia, The Free Encyclopedia. https://en.wikipedia.org/wiki/XAMPP