Core PHP 5 programs with output 15 Q/A 5 MCQs

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

  • Explain what PHP is, who created it and how it has evolved up to PHP 8.
  • Describe what a PHP file is and why PHP is so widely used.
  • Write PHP scripting blocks and embed them in HTML.
  • Output text with echo, print and the short echo tag <?=.
  • Add comments and understand which parts of PHP are case-sensitive.
What is PHP?

PHP is a recursive acronym for PHP: Hypertext Preprocessor. It is a server-side scripting language whose main purpose is to generate the HTML that is viewed by the client. The PHP code itself is never visible to the client: if you choose View Source in your browser you see only the resulting HTML, not the PHP code. This is different from client-side languages such as JavaScript, whose code is downloaded to and visible in the browser.

PHP was created by Rasmus Lerdorf in 1995 and saw its first official releases soon after. Today a very large share of the world's web sites – including WordPress, Wikipedia (MediaWiki) and many large-scale applications – run on PHP.

VersionMilestone
PHP/FI (1995)Rasmus Lerdorf's "Personal Home Page Tools".
PHP 3 (1998)Rewritten by Andi Gutmans and Zeev Suraski; the name becomes "PHP: Hypertext Preprocessor".
PHP 4 (2000)Powered by the new Zend Engine; sessions and output buffering.
PHP 5 (2004)Zend Engine 2 – a full object model (classes, interfaces, exceptions), MySQLi, PDO, SimpleXML.
PHP 7 (2015)Much faster engine, scalar type declarations, ?? and <=> operators; old mysql_* and ereg functions removed.
PHP 8 (2020 →)JIT compiler, named arguments, union types, match, nullsafe ?->, enums (8.1), readonly classes (8.2). XAMPP 8.2.12 ships PHP 8.2.
PHP in short
  • PHP stands for PHP: Hypertext Preprocessor.
  • PHP is a server-side scripting language, like ASP.NET or JSP.
  • PHP scripts are executed on the server; the browser receives plain HTML.
  • PHP supports many databases – MySQL/MariaDB, PostgreSQL, Oracle, SQLite, Microsoft SQL Server, and any database through ODBC.
  • PHP is open source software and is free to download and use.
What is a PHP file?
  • A PHP file can contain text, HTML tags and PHP scripts.
  • PHP files are returned to the browser as plain HTML.
  • PHP files have the extension .php. (Very old servers also accepted .php3 and .phtml.)
Why PHP?
  • PHP runs on many platforms – Windows, Linux, Unix, macOS.
  • PHP is compatible with almost all web servers in use today – Apache, Nginx, IIS.
  • PHP is free to download from the official resource, www.php.net.
  • PHP is easy to learn and runs efficiently on the server side.
Basic PHP Syntax

A PHP scripting block always starts with <?php and ends with ?>. A scripting block can be placed anywhere in the document.

Syntax
<?php
    statement;
    statement;
?>

A PHP file normally contains HTML tags, just like an HTML file, plus some PHP scripting code. Below is a simple PHP script that sends the text "Hello World" to the browser:

File: htdocs/phpbook/ch03/hello.php
<html>
<body>
<?php
echo "Hello World";
?>
</body>
</html>
OUTPUT browser · http://localhost/phpbook/ch03/hello.php
Hello World
  • Each code line (statement) in PHP must end with a semicolon. The semicolon is a separator that distinguishes one instruction from another. The semicolon before ?> may be omitted, but it is good practice to write it.
  • There are two basic statements to output text with PHP: echo and print.
  • The file must have a .php extension. If the file has a .html extension, the PHP code is not executed and the browser simply shows the text.

Short tags. Some old books start blocks with <? instead of <?php. This "short open tag" works only when short_open_tag is enabled in php.ini and is not portable, so always use the standard <?php. The short echo tag <?= ... ?> is different – it is always available (since PHP 5.4) and means <?php echo ... ?>.

Output with echo and print

Both echo and print are language constructs (not real functions), so parentheses are optional.

ConstructDetails
echoOutputs one or more strings separated by commas: echo "a", "b"; It has no return value and is slightly faster.
printOutputs exactly one string and always returns 1, so it can be used inside an expression.
<?= ?>Short echo tag – prints the value of an expression inside HTML: <?= $name ?>.
File: htdocs/phpbook/ch03/echo_print.php
<?php
echo "<h3>Using echo and print</h3>";
echo "Hello ", "World", "!<br>";          // echo accepts many arguments
echo("PHP is fun.<br>");                  // parentheses are optional
print "print outputs one string.<br>";
$r = print "print also returns a value: ";
echo $r . "<br>";
$x = 10;
$y = 20;
echo "Sum of $x and $y is " . ($x + $y);
?>
OUTPUT browser · http://localhost/phpbook/ch03/echo_print.php
Using echo and print
Hello World!
PHP is fun.
print outputs one string.
print also returns a value: 1
Sum of 10 and 20 is 30
Mixing PHP and HTML

PHP can switch in and out of HTML as often as needed. Everything outside <?php ... ?> is sent to the browser unchanged.

File: htdocs/phpbook/ch03/mix.php
<?php $title = "Courses"; $fee = 4500; ?>
<!DOCTYPE html>
<html>
<head><title><?= $title ?></title></head>
<body>
  <h2><?= $title ?> offered this month</h2>
  <ul>
    <li>Core PHP – fee Rs. <?= $fee ?></li>
    <li>Advance PHP – fee Rs. <?php echo $fee + 1500; ?></li>
  </ul>
  <p>Page generated on <?= date("d-m-Y") ?>.</p>
</body>
</html>
OUTPUT browser · http://localhost/phpbook/ch03/mix.php
Courses offered this month
Core PHP – fee Rs. 4500
Advance PHP – fee Rs. 6000

Page generated on 22-10-2026.

The browser receives only HTML. If you choose View Source you will see, for example, <li>Core PHP – fee Rs. 4500</li> – the PHP code has already been replaced by its result.

Comments in PHP

Comments are ignored by PHP. Use them to explain your code to other programmers – and to yourself months later.

  • // – single-line comment (C++ style).
  • # – single-line comment (shell style).
  • /* ... */ – multi-line comment block (C style).
File: htdocs/phpbook/ch03/comments.php
<html>
<body>
<?php
// This is a single-line comment
# This is also a single-line comment
/*
   This is
   a comment
   block
*/
echo "Only this line is displayed."; // comment after code
?>
</body>
</html>
OUTPUT browser · http://localhost/phpbook/ch03/comments.php
Only this line is displayed.
Case Sensitivity and White Space
  • Keywords, function names and class names are not case-sensitive: ECHO, echo and Echo are the same; STRLEN() calls strlen().
  • Variable names are case-sensitive: $name, $Name and $NAME are three different variables.
  • Constants defined with define() are case-sensitive.
  • Extra spaces, tabs and new lines between tokens are ignored, so you can format code for readability.
File: htdocs/phpbook/ch03/case.php
<?php
$color = "red";
ECHO "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
Echo "Length of 'PHP' = " . STRLEN("PHP");
?>
OUTPUT browser · http://localhost/phpbook/ch03/case.php
My car is red

Warning: Undefined variable $COLOR in /opt/lampp/htdocs/phpbook/ch03/case.php on line 4
My house is
Length of 'PHP' = 3

The first and last lines work, because ECHO, Echo and STRLEN are case-insensitive. The second line shows a Warning because $COLOR is a different – undefined – variable from $color.

Omitting the Closing Tag

If a file contains only PHP code, the closing ?> at the end of the file may be omitted. This is recommended for pure-PHP files (classes, configuration files) because it prevents accidental white space after ?> from being sent to the browser – which can break header(), setcookie() and session_start() (see Chapter 31).

Key Takeaways
  • PHP is a free, open source, server-side scripting language created by Rasmus Lerdorf in 1995; this book uses PHP 8.
  • PHP code is executed on the server; the browser receives only HTML.
  • A script block starts with <?php and ends with ?>; every statement ends with a semicolon.
  • echo outputs one or more strings; print outputs one string and returns 1; <?= ?> echoes inside HTML.
  • Comments: //, # and /* */.
  • Keywords and function names are case-insensitive; variable names are case-sensitive.
Program File

Extract the ZIP into C:\xampp\htdocs. It creates the folder phpbook/ch03 used in the file labels above; open the programs at http://localhost/phpbook/… with Apache (and MySQL, where needed) started in the XAMPP Control Panel.

Question / Answer (15 questions)

Answer : PHP (PHP: Hypertext Preprocessor) is a free, open source, server-side scripting language used to create dynamic web pages. Scripts run on the web server and generate HTML that is sent to the browser.

Answer : PHP was created by Rasmus Lerdorf in 1995. It was later rewritten by Andi Gutmans and Zeev Suraski (PHP 3, 1998), whose Zend Engine powers PHP 4 and later.

Answer : Because PHP runs on the server. The server executes the script and sends only its output – usually HTML – to the browser, so View Source shows the generated HTML, not the PHP code.

Answer : MySQL/MariaDB, PostgreSQL, Oracle, SQLite, Microsoft SQL Server and any ODBC data source (any four).

Answer : A PHP file can contain plain text, HTML tags and PHP script blocks. It uses the .php extension so that the web server passes it to the PHP engine.

Answer : It starts with <?php and ends with ?>. A block can appear anywhere in the document and a file can contain many blocks.

Answer : The semicolon is the statement separator. Every PHP statement must end with a semicolon so that PHP can tell one instruction from the next.

Answer : The server does not pass it to the PHP engine, so the PHP code is not executed; the browser treats it as plain HTML/text.

Answer : echo can output one or more comma-separated strings and has no return value; print outputs exactly one string and always returns 1, so it can be used in expressions. echo is marginally faster.

Answer : <?= expression ?> is a shortcut for <?php echo expression; ?>. It is always available since PHP 5.4 and is convenient inside HTML templates.

Answer : It only works when short_open_tag is enabled in php.ini, so the code is not portable between servers. The standard <?php tag always works.

Answer : // single-line comment, # single-line comment, and /* ... */ multi-line comment.

Answer : Partly. Keywords, function names and class names are case-insensitive (ECHO = echo), but variable names are case-sensitive ($name and $Name are different variables). Constants are case-sensitive too.

Answer : In files that contain only PHP code. Omitting it prevents accidental white space after ?> from being sent as output, which would cause "headers already sent" errors with header(), setcookie() or session_start().

Answer : The JIT compiler, named arguments, union types, the match expression, the nullsafe operator ?-> and (in 8.1) enums – any three.
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. PHP code is executed:

Show Answer

Correct Answer : B. On the web server
PHP is server-side; only its output reaches the browser.

Q2. Which tag correctly starts a portable PHP block?

Show Answer

Correct Answer : A. <?php
<?php always works; <? needs short_open_tag enabled.

Q3. Which statement about print is TRUE?

Show Answer

Correct Answer : B. It always returns 1
print outputs one string and returns 1.

Q4. Which of these is case-sensitive in PHP?

Show Answer

Correct Answer : C. Variable names
$color and $COLOR are different variables; keywords and function names are not case-sensitive.

Q5. Which symbol does NOT start a comment in PHP?

Show Answer

Correct Answer : D. <!--
<!-- is an HTML comment and would be sent to the browser.

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. The PHP Group. (n.d.). PHP Manual – What is PHP? https://www.php.net/manual/en/intro-whatis.php
  2. The PHP Group. (n.d.). PHP Manual – History of PHP. https://www.php.net/manual/en/history.php.php
  3. The PHP Group. (n.d.). PHP Manual – Basic syntax: PHP tags. https://www.php.net/manual/en/language.basic-syntax.phptags.php
  4. The PHP Group. (n.d.). PHP Manual – Comments. https://www.php.net/manual/en/language.basic-syntax.comments.php
  5. The PHP Group. (n.d.). PHP Manual – echo. https://www.php.net/manual/en/function.echo.php