Alright, let’s build a complete beginner-to-advanced guide to PHP step by step.
This will give you
everything you need to understand and work with PHP.
PHP Complete Guide
1. Introduction to PHP
• PHP = Hypertext Preprocessor.
• A server-side scripting language.
• Used for building dynamic websites and web applications.
• Works with HTML, CSS, JavaScript, and databases (like MySQL).
2. Setting Up PHP
• Install XAMPP, WAMP, or MAMP (bundled with Apache, PHP, MySQL).
• Save files with .php extension.
• Run through local server: http://localhost/filename.php
3. PHP Basics
Hello World
<?php
echo "Hello, World!";
?>
Comments
// Single line comment
/* Multi-line comment */
Variables
<?php
$name = "Riya";
$age = 16;
1
echo $name;
?>
Data Types
• String, Integer, Float, Boolean, Array, Object, NULL.
Constants
<?php
define("SITE", "MyWebsite");
echo SITE;
?>
4. Operators
• Arithmetic: + - * / %
• Assignment: = += -=
• Comparison: == != > < >= <=
• Logical: && || !
5. Control Structures
If-Else
if($age >= 18){
echo "Adult";
}else{
echo "Minor";
}
Switch
switch($grade){
case 'A': echo "Excellent"; break;
case 'B': echo "Good"; break;
default: echo "Try Again";
}
2
Loops
for($i=1; $i<=5; $i++){
echo $i;
}
while($i<=5){
echo $i;
$i++;
}
6. Functions
function greet($name){
return "Hello, $name";
}
echo greet("Aarav");
7. Arrays
Indexed Array
$fruits = ["Apple", "Banana", "Mango"];
echo $fruits[0];
Associative Array
$marks = ["Riya"=>90, "Aarav"=>80];
echo $marks["Riya"];
Multidimensional Array
$students = [
["Name"=>"Riya", "Age"=>16],
["Name"=>"Aarav", "Age"=>17]
];
3
Array Functions
• count() , array_push() , array_merge() , sort() .
8. Strings
$str = "Hello World";
echo strlen($str);
echo str_replace("World","PHP",$str);
echo strtolower($str);
echo strtoupper($str);
9. Forms and User Input
HTML Form
<form method="POST" action="process.php">
<input type="text" name="username">
<input type="submit">
</form>
PHP Handling
<?php
$name = $_POST['username'];
echo "Welcome, $name";
?>
10. File Handling
$file = fopen("data.txt","w");
fwrite($file, "Hello PHP");
fclose($file);
$file = fopen("data.txt","r");
echo fread($file, filesize("data.txt"));
fclose($file);
4
11. Sessions and Cookies
Sessions
session_start();
$_SESSION['user'] = "Riya";
echo $_SESSION['user'];
Cookies
setcookie("user", "Aarav", time()+3600);
echo $_COOKIE['user'];
12. Working with MySQL (via MySQLi)
Connect
$conn = new mysqli("localhost","root","","school");
if($conn->connect_error){ die("Connection failed"); }
Insert
$sql = "INSERT INTO Students (Name, Age, Grade) VALUES ('Riya',16,'A')";
$conn->query($sql);
Select
$result = $conn->query("SELECT * FROM Students");
while($row = $result->fetch_assoc()){
echo $row['Name'];
}
13. Object-Oriented PHP
class Student {
public $name;
5
function __construct($n){ $this->name = $n; }
function getName(){ return $this->name; }
}
$s1 = new Student("Riya");
echo $s1->getName();
14. Error Handling
try{
throw new Exception("Error Occurred");
}catch(Exception $e){
echo $e->getMessage();
}
15. Advanced PHP
• Namespaces
• Traits
• Interfaces & Abstract Classes
• Dependency Injection
• Composer (package manager)
• MVC Frameworks (Laravel, CodeIgniter, Symfony)
✅ PHP Roadmap
1. Learn basics (variables, operators, control structures).
2. Practice arrays, strings, and functions.
3. Work with forms, sessions, and cookies.
4. Connect PHP with MySQL.
5. Learn OOP in PHP.
6. Explore advanced concepts & frameworks (Laravel).