PHP (Hypertext Preprocessor) – Notes
1. Introduction
PHP is a server-side scripting language.
Mainly used for web development to create dynamic web pages.
Can interact with databases (like MySQL).
Free and open-source.
2. Features of PHP
Server-side scripting – Code runs on server.
Cross-platform – Works on Windows, Linux, Mac.
Database Support – MySQL, PostgreSQL, Oracle, SQLite.
Easy to learn – Syntax similar to C, Java, Perl.
Open-source – Free to use and distribute.
Fast execution – Efficient for web apps.
3. PHP Syntax
PHP code is written inside
<?php ... ?>tags.
<?php
echo "Hello World";
?>
echoorprintis used to display output.
4. PHP Variables
Variables start with
$symbol.Case-sensitive.
Example:
$name = "Dinesh";
$age = 25;
Data types:
String:
"Hello"Integer:
10Float:
10.5Boolean:
true/falseArray:
array(1,2,3)Object
5. PHP Constants
Value cannot change once defined.
define("PI", 3.14);
echo PI;
6. PHP Operators
Arithmetic:
+ - * / % **Assignment:
= += -= *= /= %=Comparison:
== != === !== > < >= <=Logical:
&& || !Increment/Decrement:
++ --String:
.(concatenation)
7. PHP Strings
Concatenation:
$first = "Dinesh";
$last = "Pagare";
echo $first . " " . $last;
String functions:
strlen(),strtoupper(),strtolower(),substr()
8. PHP Arrays
Indexed Array
$fruits = array("Apple","Banana","Orange");
echo $fruits[0]; // Apple
Associative Array
$age = array("Dinesh"=>25,"Ravi"=>30);
echo $age["Dinesh"];
Multidimensional Array
$students = array(
array("Dinesh",25),
array("Ravi",30)
);
echo $students[0][0]; // Dinesh
Array functions:
count(),array_push(),array_pop(),sort(),array_merge()
9. PHP Conditional Statements
if($age > 18) {
echo "Adult";
} else if($age > 12) {
echo "Teenager";
} else {
echo "Child";
}
switch($day) {
case "Mon": echo "Monday"; break;
case "Tue": echo "Tuesday"; break;
default: echo "Other day";
}
10. PHP Loops
while loop
$i = 0;
while($i < 5){
echo $i;
$i++;
}
do-while loop
$i = 0;
do {
echo $i;
$i++;
} while($i < 5);
for loop
for($i=0; $i<5; $i++){
echo $i;
}
foreach loop (for arrays)
$fruits = array("Apple","Banana","Orange");
foreach($fruits as $fruit){
echo $fruit;
}
11. PHP Functions
Reusable block of code.
function greet($name){
return "Hello " . $name;
}
echo greet("Dinesh");
Default parameters:
function greet($name="Guest"){
return "Hello ".$name;
}
12. PHP Superglobals
Predefined variables available in all scopes:
$_GET– Data sent via URL$_POST– Data sent via form$_REQUEST– Combination of GET and POST$_SESSION– Session variables$_COOKIE– Cookies$_SERVER– Server info$_FILES– File upload info
13. PHP Forms
HTML Form
<form method="post" action="process.php">
Name: <input type="text" name="name">
<input type="submit" value="Submit">
</form>
Process in PHP
$name = $_POST['name'];
echo "Hello ".$name;
14. PHP File Handling
Open file
$file = fopen("test.txt","r");
Read file
echo fread($file, filesize("test.txt"));
Write file
$file = fopen("test.txt","w");
fwrite($file,"Hello World");
fclose($file);
15. PHP Sessions & Cookies
Session
session_start();
$_SESSION['user']="Dinesh";
Cookie
setcookie("user","Dinesh",time()+3600); // 1 hour
16. PHP Object-Oriented Programming (OOP)
Class & Object
class Car {
public $color;
function setColor($c){ $this->color=$c; }
function getColor(){ return $this->color; }
}
$myCar = new Car();
$myCar->setColor("Red");
echo $myCar->getColor();
Inheritance
class Vehicle { }
class Car extends Vehicle { }
Constructor & Destructor
class Car {
function __construct(){ echo "Car created"; }
function __destruct(){ echo "Car destroyed"; }
}
17. PHP Database (MySQL)
Connect to MySQL
$conn = mysqli_connect("localhost","root","","mydb");
if(!$conn) { die("Connection failed: ".mysqli_connect_error()); }
Query
$sql = "SELECT * FROM users";
$result = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($result)){
echo $row['name'];
}
18. Important Notes
PHP files must have
.phpextensionCode executes on server, not client
Case-sensitive for variables & functions
Comments:
// single line
/* multi-line */
