PHP: Hypertext Preprocessor

Chapter 4 · Tamil Nadu State Board

  • 10 min read
  • 5-question quiz

Every key point below shows the sentence from your textbook it comes from. How we check content

In short

This lesson explains PHP, a server-side scripting language used to build dynamic web pages and web applications. It introduces PHP syntax, tags, variables, data types, comments, and the echo statement. It also teaches how PHP works with HTML and describes several types of operators. These ideas help students understand how PHP code is processed and how it can create and display web content.

Written by AI from this lesson. How we check it

Key points

  • PHP runs on the server, which processes its code before sending the webpage to the browser.
    From your textbook
    “PHP is a server-side scripting language, which means that the code is executed on the server rather than in the user’s browser.”
  • A dynamic webpage can show content that changes between views, unlike a static page with fixed content.
    From your textbook
    “A dynamic web page, on the other hand, is a page whose content can change each time it is viewed.”
  • PHP can be combined with HTML, JavaScript, and CSS to make interactive web applications.
    From your textbook
    “PHP is often used in combination with HTML, JavaScript, and CSS to create dynamic and interactive web applications.”
  • PHP code is enclosed in PHP tags, and each PHP statement must end with a semicolon.
    From your textbook
    “A PHP statement must end with a semicolon (;).”
  • PHP variable names start with a dollar sign and are case-sensitive.
    From your textbook
    “Variable names in PHP are casesensitive.”
  • PHP has eight data types, including Integer, Float, String, Boolean, Array, Object, Resource, and NULL.
    From your textbook
    “The following are the eight data types:”
  • The echo statement displays strings, variable values, or expression results.
    From your textbook
    “In PHP, the echo statement is used to output a string or other data to the screen.”
Test yourself: 5 questions

PHP and Dynamic Web Pages

PHP stands for PHP: Hypertext Preprocessor. Rasmus Lerdorf created it in 1994. PHP runs on a server and is widely used for web development.

Definition

Dynamic web page
A dynamic web page can show content that changes each time it is viewed.

Definition

Static web page
A static web page has fixed content that stays the same each time it is viewed unless a web developer updates it.

Static and dynamic web pages

Static web pageDynamic web page
Content is fixed unless manually updated.Content can change each time the page is viewed.

PHP is an interpreted language. The web server interprets PHP files line by line, so they do not need to be compiled into an executable file. PHP is especially useful for websites that interact with databases.

PHP Features and Web Development Tools

Main features of PHP

  • Server-side scripting: the server runs the scripts and sends the output to the web browser.
  • Open-source software: anyone can view and modify its source code.
  • Platform independent: it can run on Windows, Linux, and macOS.
  • Database support: it can be used to create database-driven websites.

Roles of web development languages

LanguageMain role
PHPCreates dynamic and interactive web applications.
HTMLStructures and formats web page content.
JavaScriptAdds interactivity to web pages.
CSSControls a page’s appearance, including its layout, font, and color.

PHP Syntax, Tags, and Variables

Basic PHP syntax rules

  • Write PHP code between the opening tag <?php and closing tag ?>.
  • End each PHP statement with a semicolon (;).

The statement echo “Hello, World!”; displays the message and ends with a semicolon. PHP supports three tag sets: PHP Default tags, Short open tags, and HTML script embed tags.

PHP tag sets

Tag setOpening and closing tags
PHP Default tags<?php and ?>
Short open tags<? and ?>
HTML script embed tags<script language=“php”> and </script>

Definition

Variable
A variable is a named location in memory where a value can be stored, and its name begins with a dollar sign ($).

Variable naming rules

  • A variable name starts with $.
  • After $, the first character must be a letter or an underscore, not a number.
  • A name can contain letters, numbers, and underscores.
  • Variable names are case-sensitive, so $name and $Name are different.
  • Valid names include $name, $age, $is_admin, and $_hidden.
  • Invalid names include $1st_name, $full-name, and $full name.

Declare a PHP variable by assigning a value to it. You do not need to state its type; PHP determines the type from the value. For example, $name = “Balu”; assigns a string, while $age = 30; assigns an integer.

PHP Data Types

Eight PHP data types

  • Integer: whole numbers that can be positive, negative, or zero. Examples: $age = 30; and $count = -10;.
  • Float: decimal numbers. Examples: $price = 19.99; and $weight = 65.5;.
  • String: characters enclosed in single or double quotes. Examples: $name = “Barath”; and $message = ‘Hello, World!’;.
  • Boolean: either true or false. Examples: $is_admin = true; and $is_logged_in = false;.
  • Array: multiple values stored in one variable. Examples: $prices = array(10, 20, 30); and $items = array(“item1”, “item2”, “item3”);.
  • Object: a value representing an instance of a class. Examples: $user = new User(); and $product = new Product();.
  • Resource: a value representing a connection to an external resource, such as a file or database. Examples: $fp = fopen(“file.txt”, “r”); and $conn = mysqli_connect(“localhost”, “username”, “password”, “database”);.
  • NULL: a special value meaning no value. The keyword NULL is not case sensitive. Examples: $x = null; and $y = “ ”;.

Remember: Remember all eight data types: Integer, Float, String, Boolean, Array, Object, Resource, and NULL.

PHP Comments

Comments add explanations or notes to PHP code. The PHP interpreter ignores them. PHP has single-line and multi-line comments.

Comment types

TypeHow to write it
Single-lineStart the comment with # or //.
Multi-linePlace the comment between /* and */.

Worked example

How can you write a single-line comment and a multi-line comment?

Try it first, then show the solution
  1. Use # or // before a single-line comment.
  2. Enclose a multi-line comment in /*...*/.

Answer: # This is a comment // This is also a comment /*This is an example of a multi-line comment. */

Displaying Output with echo

Definition

echo statement
A PHP statement that displays a string or other data on the screen.

Ways to use echo

  • Display one string in single or double quotes: echo ‘Hello, world!’;
  • Display a variable: $name = “Kumar”; echo “My name is $name”;
  • Display an expression: $x = 10; $y = 20; echo $x + $y; outputs 30.
  • Display several variables by separating them with commas: echo $x, $y, $z;
  • Join strings with the concatenation operator (.): echo “My name is” . $name;
  • Display text on multiple lines using multiple echo statements.
  • Use double quotes around a string that contains a single quote: echo “It’s a nice day today.”;

Parentheses are not required around the values used with echo. You may include them, as in echo(“Hello, World!”);. This has the same effect as echo “Hello, World!”;

A simple PHP script can display Hello, World! in a web browser. Save the script with a .php extension in a location accessible to a web server. The <?php and ?> tags mark the start and end of a PHP block.

PHP Operators

Definition

Operator
A symbol that performs an action on one or more values, which are called operands.

Types of PHP operators

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Increment/decrement operators
  • String operators

Arithmetic operators

OperatorActionExample and output
+Addition5+2 gives 7
-Subtraction5-2 gives 3
*Multiplication5*2 gives 10
/Division5/2 gives 2.5
%Modulus; returns the remainder5%2 gives 1

The asterisk (*) is the multiplication operator in PHP. The modulus operator (%) gives the remainder after division.

The assignment operator = assigns the value on its right to the variable on its left. Combined assignment operators perform an operation and assign the result back to the variable. For example, $a += $b is equivalent to $a = $a + $b.

Comparison operators

OperatorMeaningExample result
>Left value is greater than the right value5>2 is True
<Left value is less than the right value5<2 is False
>=Left value is greater than or equal to the right value5>=2 is True
<=Left value is less than or equal to the right value5<=2 is False
==Values are equivalent5==5.0 is True
===Values are equivalent and have the same type5===5.0 is False
!=Values are not equivalent5!=6 is True
!==Values are equivalent but are not the same type5!==5.0 is True

Logical operators

OperatorMeaningExample result
&&True only when both operands are true(5>3) && (5>7) is False
||True when at least one operand is true(5>3) || (5>7) is True
xorTrue when exactly one operand is true(5>3) xor (5>7) is True
!Reverses the operand's truth value!(5>3) is False

Increment (++) increases a variable's value by 1. Decrement (--) decreases it by 1. With a prefix operator, the variable changes before its value is returned. With a postfix operator, its value is returned before it changes.

String operators

OperatorActionExample result
.Joins two strings$c = $a . $b; gives HelloWorld when $a is ‘Hello’ and $b is ‘World’.
.=Joins a string and assigns the result to a variable$a .= $b; gives HelloWorld when $a is ‘Hello’ and $b is ‘World’.

Remember: For comparison questions, remember that == checks whether values are equivalent, while === also checks that they are the same type.

Embedding PHP Code in HTML

PHP code can be placed inside the body of an HTML file. Enclose PHP code between <?php and ?> tags. The PHP interpreter treats the code between these tags as PHP and executes it.

Basic steps

  • Write the HTML page structure.
  • Place PHP code where needed inside the <body> element.
  • Enclose the PHP code in <?php and ?> tags.

For example, the echo statement can display “Hello, World!” on the webpage when the PHP script runs.

How a PHP Script Works

What happens when a page is requested

  • The client requests a webpage from the server.
  • The server processes the request and sends any PHP code to the PHP interpreter.
  • The PHP interpreter runs the code and creates output.
  • The server combines that output with the rest of the HTML to make the webpage.
  • The server sends the completed webpage to the client’s browser.
  • The browser displays the webpage for the user.

Where the PHP code runs

ServerClient
Runs the PHP script and produces its output.Receives and displays the completed webpage.
Processes PHP before sending the page.Does not need special software to run PHP.

Remember: PHP runs on the server. The browser receives the output, not the PHP code itself.

PHP Terms and Basic Syntax

Definition

PHP
PHP stands for PHP: Hypertext Preprocessor and is a server-side scripting language.

Definition

Echo
The echo statement displays a string of text or a variable in PHP.

Definition

WAMP
WAMP is an abbreviation for “Windows, Apache, MySQL, and PHP”.

PHP rules and features

  • PHP code is enclosed in <?php and ?> tags.
  • A variable name begins with a dollar sign ($), for example, $name.
  • PHP has eight data types.
  • PHP has single-line and multi-line comments.
  • Single-line comments begin with two forward slashes (//) or a single hash sign (#).
  • Multi-line comments begin with /* and end with */.
  • The assignment operator = assigns a value to a variable.
  • The concatenation operator is a single dot.

Questions students ask

Short, direct answers to what students search for in this chapter.

What is PHP and where does it run?

PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language, so its code is executed on the server rather than in the user’s browser. The server sends the output to the web browser.

What is the difference between a static and dynamic web page?

A static web page has fixed content that stays the same each time it is viewed unless a web developer updates it. A dynamic web page can show content that changes each time it is viewed.

What are the basic PHP syntax rules?

Enclose PHP code between the opening tag <?php and the closing tag ?>. End each PHP statement with a semicolon (;). For example, echo “Hello, World!”; is a valid PHP statement.

What are the eight data types in PHP?

The eight PHP data types are Integer, Float, String, Boolean, Array, Object, Resource, and NULL. They describe different kinds of values, such as whole numbers, decimal numbers, characters, true or false values, and multiple values stored in one variable.

What are the rules for naming PHP variables?

A PHP variable name starts with a dollar sign ($). Its first character after $ must be a letter or underscore, and its name can contain letters, numbers, and underscores. Names are case-sensitive, so $name and $Name are different variables.

What are comments in PHP?

Comments are notes or explanations added to PHP code. The PHP interpreter ignores them. Write a single-line comment by starting it with # or //. Write a multi-line comment by enclosing it between /* and */.

Show 9 more questions

How do you display text in PHP using echo?

Use echo followed by the string or data you want to display. Put strings in single or double quotes. You can also display variables or expressions, separate multiple variables with commas, and join strings using the concatenation operator (.). Parentheses around echo arguments are optional.

What is the difference between == and === in PHP?

The == operator checks whether two values are equivalent. The === operator checks whether they are equivalent and are also the same type. For example, 5==5.0 is True, but 5===5.0 is False.

What do increment and decrement operators do in PHP?

The increment operator ++ increases a variable's value by 1, and the decrement operator -- decreases it by 1. A prefix operator changes the variable before returning its value. A postfix operator returns the value first and changes the variable afterward.

What are the string operators in PHP?

PHP has the concatenation operator (.) and the concatenation assignment operator (.=). The dot joins two strings. The dot-equal operator joins a string to another and assigns the result back to the variable in the same statement.

How do you embed PHP code in HTML?

Place the PHP code inside the HTML body and enclose it between <?php and ?> tags. The PHP interpreter executes the code between these tags. For example, an echo statement can display “Hello, World!” on the webpage.

How does a PHP script work?

The client requests a webpage from the server. The server sends embedded PHP code to the PHP interpreter, which runs it and creates output. The server combines this output with the HTML and sends the completed webpage to the client’s browser.

Does PHP run on the server or in the browser?

PHP runs on the server, before the webpage is sent to the client’s browser. The browser receives and displays the completed webpage, rather than the PHP code itself. A standard web browser is enough to view the page.

How do PHP single-line and multi-line comments begin and end?

A single-line PHP comment begins with two forward slashes (//) or a single hash sign (#). A multi-line comment begins with /* and ends with */. These are the two types of comments described for PHP.

What is the echo statement used for in PHP?

The echo statement is used to display a string of text or a variable in PHP. For example, an echo statement can output “Hello, World!” on a webpage when the PHP script is executed.

Practice

Question 1 of 5Score 0

Where does PHP code run when a PHP webpage is requested?

Exam questions

Practice questions with model answers. These are not from an official paper.

What is PHP, and where is its code executed?2 marks

PHP stands for PHP: Hypertext Preprocessor. It is a server-side scripting language, so its code is executed on the server rather than in the user’s browser.

Give two rules for naming variables in PHP.2 marks

A variable name must begin with a dollar sign. The first character after it must be a letter or underscore, not a number. Variable names are also case-sensitive.

Name the eight data types in PHP.2 marks

The eight data types are Integer, Float, String, Boolean, Array, Object, Resource, and NULL.

Explain how a PHP script embedded in HTML produces a webpage.5 marks

PHP is a server-side scripting language used to create dynamic web pages. When PHP is embedded in HTML, the server executes the PHP code before sending the webpage to the browser.

  1. Client request: The process begins when the client requests a webpage from the server.
  2. Server processes the request: The server receives and processes the request. If the page contains PHP code, it passes that code to the PHP interpreter.
  3. PHP code is executed: The PHP interpreter executes the code and generates output on the server.
  4. Output forms the webpage: The output from PHP is combined with the remaining HTML to create the final webpage.
  5. Completed page is sent: The server sends the completed webpage to the client’s browser. The browser receives and renders it for the user.
  6. PHP code remains on the server: The browser sees the PHP output, not the PHP code itself, because PHP scripts are executed on the server.

Example: The chapter’s example outputs “Hello, World!” on the webpage when the PHP script is executed.

Thus, the server executes the embedded PHP code, combines its output with HTML, and sends the finished webpage to the browser.

Watch out

  • Do not confuse the assignment operator = with the comparison operators == and ===.
  • Do not assume a static webpage changes content between views; the lesson describes its content as fixed unless manually updated.

Still stuck on this lesson?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →