Introduction to PHP
1. What is PHP
-
PHP (Hypertext Preprocessor) is a server-side scripting language used to build dynamic web applications.
-
It is executed on the server and returns HTML output to the browser.
-
Widely used with databases like MySQL for full-stack web development.
Key Features
-
Open source
-
Platform independent
-
Embedded in HTML
-
Supports databases (MySQL, PostgreSQL, etc.)
-
Interpreted language (no compilation required)
2. Installing and Configuring PHP
2.1 Using XAMPP (Recommended for beginners)
-
Download XAMPP
-
Install and open XAMPP Control Panel
-
Start:
-
Apache (Web Server)
-
MySQL (Database)
-
-
Place PHP files inside:
C:\xampp\htdocs\
- Run in browser:
http://localhost/filename.php
2.2 Verify Installation
Create file: test.php
<?php
phpinfo();
?>Open in browser → displays PHP configuration details.
3. Building Blocks of PHP
3.1 PHP Tags
PHP code is written inside special tags.
Types of PHP Tags
- Standard Tag (Recommended)
<?php
echo "Hello World";
?>- Short Tag (Not always enabled)
<?
echo "Hello";
?>- Echo Tag
<?= "Hello World"; ?>3.2 Variables in PHP
Definition
Variables are used to store data.
Rules
-
Starts with
$ -
Must begin with letter or underscore
-
Cannot start with number
-
Case-sensitive
Example
<?php
$name = "Shivam";
$age = 21;
echo $name;
echo $age;
?>3.3 Data Types in PHP
Types
- String
$name = "PHP";- Integer
$age = 21;- Float (Double)
$price = 99.99;- Boolean
$isActive = true;- Array
$colors = array("red", "blue", "green");- NULL
$x = null;3.4 Constants
Definition
Constants are values that cannot be changed once defined.
Syntax
define("PI", 3.14);
echo PI;3.5 Operators in PHP
1. Arithmetic Operators
$a = 10;
$b = 5;
echo $a + $b; // 15
echo $a - $b; // 5
echo $a * $b; // 50
echo $a / $b; // 22. Assignment Operator
$x = 10;
$x += 5; // x = 153. Comparison Operators
$a == $b // equal
$a != $b // not equal
$a > $b
$a < $b4. Logical Operators
($a > 5 && $b < 10)
($a > 5 || $b < 10)
!($a > 5)3.6 Expressions
Definition
An expression is a combination of variables, values, and operators that produces a result.
$result = ($a + $b) * 2;4. Control Structures
4.1 Conditional Statements
1. if Statement
<?php
$age = 18;
if ($age >= 18) {
echo "Eligible to vote";
}
?>2. if-else Statement
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}3. if-elseif-else
$marks = 75;
if ($marks >= 90) {
echo "A Grade";
} elseif ($marks >= 60) {
echo "B Grade";
} else {
echo "Fail";
}4.2 Switch Statement
Definition
Used when multiple conditions are based on same variable.
$day = 2;
switch ($day) {
case 1:
echo "Monday";
break;
case 2:
echo "Tuesday";
break;
default:
echo "Invalid";
}4.3 Loops in PHP
1. for Loop
for ($i = 1; $i <= 5; $i++) {
echo $i;
}2. while Loop
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}3. do-while Loop
$i = 1;
do {
echo $i;
$i++;
} while ($i <= 5);4. foreach Loop (for arrays)
$colors = ["red", "blue", "green"];
foreach ($colors as $color) {
echo $color;
}5. Complete Working Program
Problem: Student Result System
Features Covered
-
Variables
-
Data types
-
Constants
-
Operators
-
Conditional statements
-
Switch
-
Loops
<?php
// Constant
define("PASS_MARKS", 40);
// Variables
$name = "Shivam";
$marks = [85, 72, 60, 30, 90];
// Calculate total
$total = 0;
foreach ($marks as $m) {
$total += $m;
}
// Calculate average
$average = $total / count($marks);
// Output
echo "Name: $name <br>";
echo "Total Marks: $total <br>";
echo "Average: $average <br>";
// Result using if-else
if ($average >= 75) {
$grade = "A";
} elseif ($average >= 60) {
$grade = "B";
} elseif ($average >= PASS_MARKS) {
$grade = "C";
} else {
$grade = "Fail";
}
echo "Grade: $grade <br>";
// Switch example
switch ($grade) {
case "A":
echo "Excellent Performance";
break;
case "B":
echo "Good Performance";
break;
case "C":
echo "Average Performance";
break;
default:
echo "Needs Improvement";
}
// Loop example
echo "<br>Marks:<br>";
for ($i = 0; $i < count($marks); $i++) {
echo "Subject " . ($i + 1) . ": " . $marks[$i] . "<br>";
}
?>6. Summary
-
PHP is a server-side scripting language for web development
-
Installed easily using XAMPP
-
Core building blocks:
-
Tags
-
Variables
-
Data types
-
Constants
-
Operators
-
Expressions
-
-
Control structures:
-
if, else, elseif
-
switch
-
loops (for, while, do-while, foreach)
-
-
Used to build dynamic, database-driven web applications