Learning Objectives
After completing this tutorial, you will be able to:
- Declare and use variables in PHP.
- Understand PHP data types.
- Perform arithmetic and logical operations using operators.
- Display output using echo and print.
- Accept user input through HTML forms.
- Build simple PHP applications using variables and operators.
Introduction
Every programming language store and manipulates data. PHP is no exception. Whether you are calculating a student’s GPA, processing an online order, or validating a login form, you need variables to store information, data types to define the kind of information being stored, operators to perform calculations, and input/output statements to interact with users.
In this tutorial, you will learn the building blocks of PHP programming. These concepts are used in every PHP application, from small websites to enterprise-level web systems.
Prerequisites
Before starting this tutorial, you should have:
- Installed XAMPP or Laragon.
- Installed Visual Studio Code.
- Created your first PHP project.
- Verified that PHP 8.x is running successfully.
What is a Variable?
A variable is a named storage location that holds data. In PHP, every variable begins with the dollar sign.
Example
<?php $name = "Fahmida"; echo "My name is $name"; ?>
Output:

The variable named $name stores the text “Fahmida”
Rules for Naming Variables
- A variable must begin with the dollar sign.
- The first character after the dollar sign must be a letter or underscore.
- Variable names cannot contain spaces.
- Variable names are case-sensitive.
Good examples
$studentName, $totalMarks, $email, $userAge
Poor examples
$1name, $student name, $total-marks
Declaring Variables
Variables do not need a specific declaration. PHP automatically determines the data type.
Example
<?php $name = "Tahsan"; $batch = 67; $cgpa = 3.89; echo $name, "<br/>", $batch, "<br/>", $cgpa; ?>
Output:

PHP Data Types
PHP supports several built-in data types.
Integer
Stores whole numbers.
<?php $age = 21; echo $age; ?>
Float
Stores decimal numbers.
<?php $price = 250.75; echo $price; ?>
String
Stores text.
<?php $department = "Computer Science"; echo $department; ?>
Boolean
Stores either true or false.
<?php $isPassed = true; ?>
Array
Stores multiple values.
<?php $subjects = ["PHP","HTML","CSS"]; print_r($subjects); ?>
Checking Data Types
PHP provides the var_dump() function.
Example-1:
<?php $age = 20; var_dump($age); ?>
Output:

Example-2:
<?php $name = "Hasan"; var_dump($name); ?>
Output:

Output Statements
PHP mainly uses echo and print.
Using echo
<?php echo "Welcome to PHP"; ?>
Echo can display multiple values.
<?php $name = "Nadia"; $age = 21; echo $name; echo "<br>"; echo $age; ?>
Using print
<?php print "Learning PHP is fun."; ?>
Both echo and print produce output. Echo is slightly faster and more commonly used.
String Concatenation
PHP joins strings using the period operator.
Example
<?php $firstName = "Farhana"; $lastName = "Rahman"; echo $firstName . " " . $lastName; ?>
Output:

Arithmetic Operators
Arithmetic operators perform mathematical calculations.
<?php // Addition echo "Addition: 20+10 = ", 20 + 10, "<br/>"; //Subtraction echo "Subtraction: 20-10 = ", 20 - 10 , "<br/>"; //Multiplication echo "Multiplication: 20x10 = ", 20 * 10, "<br/>"; //Division echo "Division: 20/10 = ", 20 / 10, "<br/>"; //Modulus echo "Modulus: 20%10 = ", 20 % 3, "<br/>"; //Exponentiation echo "Exponentiation: 2 ** 5 = ", 2 ** 5, "<br/>"; ?>
Output:

Assignment Operators
Assignment operators assign values to variables.
<?php $x = 10; $x += 5; echo "$x<br/>"; $x *= 10; echo "$x<br/>"; ?>
Output:

Comparison Operators
Comparison operators compare two values.
- Equal to: $a == $b
- Not equal to: $a != $b
- Greater than: $a > $b
- Less than: $a < $b
- Greater than or equal: $a >= $b
- Less than or equal: $a <= $b
Example
<?php $x = 20; $y = 15; var_dump($x > $y); ?>
Output:
bool(true)
Logical Operators
Logical operators combine conditions.
- AND, &&
- OR, ||
- NOT, !
Example
<?php $age = 22; $isStudent = true; var_dump($age > 18 && $isStudent); ?>
Output:
bool(true)
Input from an HTML Form
Create a file named index.php.
<!DOCTYPE html> <html> <head> <title>PHP Input Example</title> </head> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="username"> <br><br> <input type="submit" value="Submit"> </form> </body> </html>
Output:

Create another file named welcome.php.
<?php $name = $_POST["username"]; echo "Welcome " . $name; ?>
When the user submits the form, PHP receives the input and displays the entered name.

Lab Tasks:
Task 1:
Create variables for your name, department, university, and semester.
Display all information.
Task 2:
Write a program to calculate the sum, difference, product, and quotient of two numbers.
Task 3:
Write a program to calculate the area of a circle.
Task 4:
Create an HTML form that accepts a student’s name.
Display the submitted name using PHP.
Task 5:
Create variables representing five subject marks.
Calculate the total.
Challenge Problems
- Create a temperature converter from Celsius to Fahrenheit.
- Create a program to calculate simple interest.
- Create a program that calculates the average of five numbers.
- Create a shopping bill calculator using variables.
Best Practices
- Use meaningful variable names.
- Keep variable names consistent.
- Indent your code properly.
- Write comments where necessary.
- Always validate user input.
- Use lowercase variable names with descriptive words.
Try yourself:
- Display your personal information using variables.
- Calculate the area and perimeter of a rectangle.
- Calculate the average marks of five subjects.
- Create an HTML form that accepts a student’s name, ID, and department.
- Build a simple calculator that performs addition, subtraction, multiplication, and division using two user-provided numbers.
- Create a program that converts years into months and days.
- Write a PHP program to calculate the total price of three products and apply a 10% discount.
- Create a personal profile page that displays all information entered through an HTML form.