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,printand 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.
| Version | Milestone |
|---|---|
| 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.php3and.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.
<?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:
<html> <body> <?php echo "Hello World"; ?> </body> </html>
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:
echoandprint. - The file must have a
.phpextension. If the file has a.htmlextension, 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.
| Construct | Details |
|---|---|
| echo | Outputs one or more strings separated by commas: echo "a", "b"; It has no return value and is slightly faster. |
| Outputs 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 ?>. |
<?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);
?>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.
<?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>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).
<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>
Only this line is displayed.
Case Sensitivity and White Space
- Keywords, function names and class names are not case-sensitive:
ECHO,echoandEchoare the same;STRLEN()callsstrlen(). - Variable names are case-sensitive:
$name,$Nameand$NAMEare 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.
<?php
$color = "red";
ECHO "My car is " . $color . "<br>";
echo "My house is " . $COLOR . "<br>";
Echo "Length of 'PHP' = " . STRLEN("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
<?phpand ends with?>; every statement ends with a semicolon. echooutputs one or more strings;printoutputs 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)
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.
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.
- The PHP Group. (n.d.). PHP Manual – What is PHP? https://www.php.net/manual/en/intro-whatis.php
- The PHP Group. (n.d.). PHP Manual – History of PHP. https://www.php.net/manual/en/history.php.php
- The PHP Group. (n.d.). PHP Manual – Basic syntax: PHP tags. https://www.php.net/manual/en/language.basic-syntax.phptags.php
- The PHP Group. (n.d.). PHP Manual – Comments. https://www.php.net/manual/en/language.basic-syntax.comments.php
- The PHP Group. (n.d.). PHP Manual – echo. https://www.php.net/manual/en/function.echo.php