PHP Tutorial for Beginners (2026): Complete Guide with Practical Examples
PHP Tutorial
PHP Tutorial remains one of the world’s most widely used server-side programming languages. Millions of websites, including WordPress, Drupal, Magento, and Laravel applications, are powered by PHP.
If you want to become a backend developer or build dynamic websites, PHP is one of the best programming languages to learn.
In this complete tutorial, you’ll learn PHP from beginner to advanced with practical examples.
Table of Contents
What is PHP?
PHP (Hypertext Preprocessor) is an open-source server-side scripting language used to create dynamic websites and web applications.
Unlike HTML, PHP executes on the server and sends only the generated HTML to the user’s browser.
Example:
Browser → Web Server → PHP Executes → HTML Output → Browser
Why Learn PHP?
PHP is still one of the best backend languages because it offers:
- Easy to learn
- Open Source & Free
- Cross-platform support
- Large developer community
- Excellent database integration
- Fast performance with PHP 8.x
- Used by WordPress (over 40% of websites)
- Works with Apache and Nginx
- Supports Object-Oriented Programming
- Huge job opportunities
Features of PHP
- Server-side scripting
- Dynamic web pages
- Database connectivity
- Session & Cookie support
- File handling
- JSON support
- REST API development
- Object-Oriented Programming
- Exception handling
- Secure password hashing
Installing PHP
Windows
Download and install:
- XAMPP
- WAMP
- Laragon
Check PHP version
php -v
Output
PHP 8.4.x
Your First PHP Program
Create
index.php
<?php
echo "Hello, PHP!";
?>
Output
Hello, PHP!
PHP Variables
<?php
$name = "John";
$age = 25;
$isStudent = true;
echo $name;
echo $age;
?>
Data Types
PHP supports:
- String
- Integer
- Float
- Boolean
- Array
- Object
- NULL
- Resource
Example
<?php
$city = "Delhi";
$price = 599.99;
$status = true;
?>
Constants
<?php
define("SITE_NAME","GuruGyaan");
echo SITE_NAME;
?>
Operators
$a = 20;
$b = 10;
echo $a + $b;
echo $a - $b;
echo $a * $b;
echo $a / $b;
If Else
<?php
$age = 18;
if($age >= 18){
echo "Eligible";
}else{
echo "Not Eligible";
}
?>
Switch Statement
<?php
$day = "Monday";
switch($day){
case "Monday":
echo "Start Work";
break;
case "Sunday":
echo "Holiday";
break;
default:
echo "Normal Day";
}
?>
Loops
For Loop
<?php
for($i=1;$i<=5;$i++){
echo $i."<br>";
}
?>
While Loop
<?php
$i=1;
while($i<=5){
echo $i;
$i++;
}
?>
Foreach Loop
<?php
$colors=["Red","Green","Blue"];
foreach($colors as $color){
echo $color."<br>";
}
?>
PHP Functions
<?php
function add($a,$b){
return $a+$b;
}
echo add(10,20);
?>
Output
30
Arrays
Indexed Array
$fruits=["Apple","Banana","Orange"];
Associative Array
$user=[
"name"=>"Vikash",
"city"=>"Panipat"
];
Multidimensional Array
$students=[
["Rahul",90],
["Amit",95]
];
String Functions
echo strlen("PHP");
echo strtoupper("hello");
echo strtolower("PHP");
echo str_replace("PHP","Laravel","Learn PHP");
Date and Time
echo date("d-m-Y");
echo date("H:i:s");
Include & Require
include "header.php";
require "config.php";
Forms
HTML
<form method="post">
<input type="text" name="username">
<input type="submit">
</form>
PHP
<?php
if(isset($_POST['username'])){
echo $_POST['username'];
}
?>
GET and POST
GET
$name=$_GET['name'];
POST
$email=$_POST['email'];
File Upload
move_uploaded_file(
$_FILES['image']['tmp_name'],
"uploads/".$_FILES['image']['name']
);
Sessions
<?php
session_start();
$_SESSION['user']="Admin";
echo $_SESSION['user'];
?>
Cookies
setcookie(
"username",
"Vikash",
time()+3600
);
Exception Handling
try{
throw new Exception("Something went wrong");
}catch(Exception $e){
echo $e->getMessage();
}
Object-Oriented PHP
Class
<?php
class Student{
public $name;
public function show(){
echo $this->name;
}
}
$obj=new Student();
$obj->name="Rahul";
$obj->show();
?>
Constructor
class User{
public function __construct(){
echo "Constructor Called";
}
}
new User();
Inheritance
class Animal{
public function sound(){
echo "Animal Sound";
}
}
class Dog extends Animal{
public function bark(){
echo "Woof";
}
}
$dog=new Dog();
$dog->sound();
$dog->bark();
Database Connection (MySQL)
<?php
$conn=new mysqli(
"localhost",
"root",
"",
"school"
);
if($conn->connect_error){
die("Connection Failed");
}
echo "Connected";
?>
Insert Data
$sql="INSERT INTO students(name)
VALUES('Rahul')";
$conn->query($sql);
Select Data
$result=$conn->query("SELECT * FROM students");
while($row=$result->fetch_assoc()){
echo $row['name'];
}
Prepared Statements (Secure)
$stmt=$conn->prepare(
"SELECT * FROM users WHERE email=?"
);
$stmt->bind_param(
"s",
$email
);
$stmt->execute();
Password Hashing
$password=password_hash(
"mypassword",
PASSWORD_DEFAULT
);
Verify
password_verify(
$mypassword,
$password
);
JSON
Encode
echo json_encode($data);
Decode
$data=json_decode($json,true);
PHP vs JavaScript
| Feature | PHP | JavaScript |
|---|---|---|
| Runs On | Server | Browser & Server |
| Primary Use | Backend | Frontend + Backend |
| Database Access | Excellent | Via APIs |
| HTML Generation | Direct | Dynamic DOM |
| WordPress Support | Native | Limited |
PHP Best Practices
- Use PHP 8.x or newer.
- Validate all user input.
- Use prepared statements.
- Escape output to prevent XSS.
- Never trust user input.
- Use Composer for dependency management.
- Follow PSR coding standards.
- Separate business logic from presentation.
- Use environment variables for secrets.
- Keep PHP and dependencies updated.
Popular PHP Frameworks
- Laravel
- Symfony
- CodeIgniter
- CakePHP
- Laminas
- Yii
Real-World Applications
PHP is used to build:
- WordPress websites
- E-commerce stores
- CRM systems
- ERP software
- REST APIs
- School Management Systems
- Blogging platforms
- Online Learning Platforms
- Admin Dashboards
- Content Management Systems
Common Interview Questions
What does PHP stand for?
PHP: Hypertext Preprocessor.
Is PHP frontend or backend?
Backend.
Is PHP free?
Yes.
Can PHP connect to MySQL?
Yes.
Which CMS uses PHP?
WordPress, Drupal, Joomla, Magento, and many others.
Conclusion
PHP continues to be one of the most powerful and beginner-friendly backend programming languages. From simple contact forms to enterprise applications, PHP can handle projects of every size. Combined with MySQL, HTML, CSS, and JavaScript, it provides a complete stack for developing dynamic and secure web applications. Learning modern PHP and following best practices will prepare you for careers in web development, freelancing, and full-stack engineering.
FAQ
Is PHP worth learning in 2026?
Yes. PHP powers a significant portion of the web and remains in high demand, especially for WordPress, Laravel, and backend development.
Which PHP version should I use?
Always use the latest stable PHP 8.x release.
Is PHP easy for beginners?
Yes. Its simple syntax makes it one of the easiest backend languages to start with.
Can I build APIs using PHP?
Yes. PHP is widely used to build secure RESTful APIs and backend services.