PHP
1. What is PHP?
·
PHP
stands for Hypertext Preprocessor.
·
It
is a server-side scripting language used to create dynamic web pages.
·
PHP
code runs on the server, not on the user's computer.
It is mainly used for:
·
Form
handling
·
Database
connection
·
Creating
dynamic websites
·
User
login systems
·
Sessions
and cookies
2. Why PHP?
·
Free
and open source
·
Easy
to learn
·
Works
on all major operating systems
·
Supports
many databases (MySQL, PostgreSQL, etc.)
·
Fast
performance
3. How PHP
Works?
·
User
sends a request from browser (e.g., opens a page).
·
Web
server (Apache/XAMPP) receives the request.
·
Server
executes PHP code.
·
Server
sends processed output (HTML only) back to browser.
4. PHP Syntax
Basics
PHP
Script Start & End
<?php
//
PHP code here
?>
Displaying
Output
echo
"Hello World!";
`echo` is used
to print text on the browser.
5. Variables in
PHP
Rules
·
Start
with `$` sign
·
Cannot
start with a number
·
Case
sensitive
Example
$name
= "Ram";
$age
= 20;
echo
$name;
6. Data Types in
PHP
String – text (e.g., "Hello")
Integer – whole numbers
Float – decimal numbers
Boolean – true/false
Array – multiple values in one variable
Object – instance of a class
NULL – no value
7. Operators
Arithmetic
Operators
`+
, - , * , / , %`
Comparison
Operators
`==
, != , > , < , >= , <=`
Logical
Operators
`&&
(AND), || (OR), ! (NOT)`
8. Conditional
Statements
If
Statement
if
($age > 18) {
echo "Adult";
}
If-else
if
($age > 18) {
echo "Adult";
}
else {
echo "Minor";
}
Else-if
if
($marks >= 80) {
echo "Distinction";
}
elseif ($marks >= 60) {
echo "First Division";
}
else {
echo "Pass";
}
9. Looping
Statements
While
Loop
$i
= 1;
while
($i <= 5) {
echo $i;
$i++;
}
For
Loop
for
($i = 1; $i <= 5; $i++) {
echo $i;
}
Foreach
Loop (for arrays)
$fruits
= ["Apple", "Mango", "Banana"];
foreach
($fruits as $f) {
echo $f;
}
10.
Functions in PHP
Creating
a Function
function
greet() {
echo "Hello!";
}
greet();
Function
with Arguments
function
add($a, $b) {
return $a + $b;
}
echo
add(5, 7);
11. Arrays in
PHP
Indexed
Array
$colors
= ["Red", "Blue", "Green"];
echo
$colors[0];
Associative
Array
$person
= ["name" => "Sita", "age" => 19];
echo
$person["name"];
Multidimensional
Array
$students
= [
["Ram", 20],
["Sita", 19]
];
echo
$students[0][0]; // Ram
12. Form
Handling in PHP
HTML
Form
html
<form
method="POST" action="process.php">
Name: <input type="text"
name="username">
<input type="submit">
</form>
PHP
Script (process.php)
$name
= $_POST['username'];
echo
"Welcome ". $name;
1. Print “Hello World”
<?php
echo "Hello World!";
?>
2. Add Two Numbers
<?php
$a = 10;
$b = 20;
$sum = $a + $b;
echo "Sum = " . $sum;
?>
3. Check Even or Odd
<?php
$num = 7;
if($num % 2 == 0)
{
echo "$num
is Even";
}
else
{
echo "$num
is Odd";
}
?>
4. Largest of Three Numbers
<?php
$a = 12;
$b = 45;
$c = 33;
if($a > $b && $a > $c){
echo "$a
is largest";
} elseif($b > $c){
echo "$b
is largest";
} else {
echo "$c
is largest";
}
?>
5. Simple Calculator (Using Switch)
<?php
$a = 10;
$b = 5;
$op = "*";
switch($op){
case
"+":
echo $a +
$b;
break;
case
"-":
echo $a -
$b;
break;
case
"*":
echo $a *
$b;
break;
case
"/":
echo $a /
$b;
break;
default:
echo
"Invalid Operator";
}
?>
6. Print Numbers from 1 to 10 (Using Loop)
<?php
for($i=1; $i<=10; $i++){
echo $i .
"<br>";
}
?>
7. Sum of Numbers from 1 to 100
<?php
$sum = 0;
for($i=1; $i<=100; $i++){
$sum += $i;
}
echo "Sum = $sum";
?>
8. Factorial of a Number
<?php
$num = 5;
$fact = 1;
for($i=1; $i<=$num; $i++){
$fact *= $i;
}
echo "Factorial of $num is $fact";
?>
9. Display Elements of an Array
<?php
$fruits = ["Apple", "Mango",
"Banana"];
foreach($fruits as $f){
echo $f .
"<br>";
}
?>
10. Function Example (Add Two Numbers)
<?php
function add($x, $y){
return $x + $y;
}
echo add(5, 7);
?>
11. Form Handling (POST Method)
<form method="POST"
action="process.php">
Name: <input
type="text" name="username">
<input
type="submit" value="Submit">
</form>
PHP File (process.php)
<?php
$name = $_POST['username'];
echo "Welcome " . $name;
?>
12. Connect to MySQL Database
<?php
$conn = mysqli_connect("localhost",
"root", "", "school");
if($conn){
echo
"Connection Successful";
} else {
echo
"Connection Failed";
}
?>
13. Insert Data into Database
<?php
$conn = mysqli_connect("localhost",
"root", "", "school");
$sql = "INSERT INTO students(name, age)
VALUES('Ram', 20)";
mysqli_query($conn, $sql);
echo "Data Inserted";
?>
14. Display Student Records
<?php
$conn = mysqli_connect("localhost",
"root", "", "school");
$result = mysqli_query($conn, "SELECT * FROM
students");
while($row = mysqli_fetch_assoc($result)){
echo
$row['name'] . " - " . $row['age'] . "<br>";
}
?>
15. Session Example
<?php
session_start();
$_SESSION['user'] = "Ram";
echo "Session Set!";
?>