Codcups

Complete PHP Programming Course

PHP (Hypertext Preprocessor) is a popular server-side scripting language used primarily for web development. It is embedded within HTML and interacts with databases, handles forms, manages sessions, and builds dynamic websites.

Introduction to PHP

PHP (Hypertext Preprocessor) is a popular server-side scripting language used primarily for web development. It is embedded within HTML and interacts with databases, handles forms, manages sessions, and builds dynamic websites.

Key Features:

Setting Up the Environment

Tools Needed:

Basic PHP Syntax


<?php
echo "Hello, World!";
?>
        

Explanation:

The basic syntax of PHP is designed to be simple, flexible, and easy to integrate with HTML...

Variables and Data Types

Declaration:


$name = "Waleed";
$age = 25;
$isOnline = true;
$price = 10.5;
        

Data Types:

In PHP, a variable is a named storage location in memory that holds a value...

Constants


define("SITE_NAME", "MyWebsite");
echo SITE_NAME;
        

Constants in PHP are identifiers used to store values that do not change during the execution of a script...

Operators

Types:

Operators in PHP are special symbols or keywords used to perform operations on variables and values...

Conditional Statements

If Statement


<?php
$age = 20;
if ($age >= 18) 
{
    echo "You are eligible to vote.";
}
?>
        

The if statement in PHP is a fundamental control structure used to execute a block of code only if a specified condition is true...

If-else Statement


if ($score >= 50) {
    echo "Passed";
} else {
    echo "Failed";
}
        

The if-else statement in PHP is a fundamental control structure used to execute specific blocks of code based on whether a condition evaluates to true or false...

If-elseif Statement


<?php
$marks = 75;

if ($marks >= 90) 
{
    echo "Grade: A+";
} elseif ($marks >= 80) 
{
    echo "Grade: A";
} elseif ($marks >= 70) 
{
    echo "Grade: B";
} elseif ($marks >= 60) 
{
    echo "Grade: C";
} else 
{
    echo "Grade: F";
}
?>
        

The if-elseif statement in PHP is used to execute different blocks of code based on multiple conditions...

Switch Statement


$day = "Monday";
switch ($day) 
{
    case "Monday":
        echo "Start of week";
        break;
    default:
        echo "Not Monday";
}
        

The switch statement in PHP is a control structure used to execute different blocks of code based on the value of a single expression...

Loops in PHP

For Loop:


for ($i = 1; $i <= 5; $i++) 
{
    echo $i;
}
        

The for loop in PHP is a control structure used to execute a block of code a specific number of times...

While Loop:


$i = 1;
while ($i <= 5) 
{
    echo $i;
    $i++;
}
        

The while loop in PHP is a control flow statement that repeatedly executes a block of code as long as a specified condition remains true...

Foreach Loop (for arrays):


$colors = ["Red", "Green", "Blue"];
foreach ($colors as $color) 
{
    echo $color;
}
        

The foreach loop in PHP is specifically designed for iterating over arrays...

Functions


function greet($name) 
{
    return "Hello, " . $name;
}
echo greet("Waleed");
        

Functions in PHP are blocks of code that perform a specific task and can be reused throughout a program...

Arrays

Indexed Array:


$fruits = ["Apple", "Banana", "Mango"];
echo $fruits[1]; // Banana
        

Associative Array:


$person = ["name" => "Ali", "age" => 30];
echo $person["name"]; // Ali
        

Multidimensional Array:


$data = [
    ["name" => "A", "marks" => 85],
    ["name" => "B", "marks" => 90]
];
        

Arrays in PHP are used to store multiple values in a single variable...

String Functions


$str = "Hello World";
echo strlen($str); // 11
echo str_replace("World", "PHP", $str); // Hello PHP
        

String functions in PHP are built-in functions used to perform various operations on strings...

Forms and User Input


Name:
<?php if ($_SERVER["REQUEST_METHOD"] == "POST") { echo "Welcome " . $_POST["username"]; } ?>

Forms are a fundamental part of web development that allow users to input data and interact with a website...

File Handling


$file = fopen("data.txt", "w");
fwrite($file, "Hello File!");
fclose($file);
        

File handling in PHP allows developers to create, read, write, and manage files directly on the server...

Error Handling


try 
{
    throw new Exception("Something went wrong!");
} catch (Exception $e) 
{
    echo "Error: " . $e->getMessage();
}
        

Error handling in PHP is the process of managing unexpected issues that occur during script execution...

Sessions and Cookies

Session:


session_start();
$_SESSION["user"] = "Waleed";
echo $_SESSION["user"];
        

Cookie:


setcookie("user", "Waleed", time() + 3600);
echo $_COOKIE["user"];
        

Sessions and cookies in PHP are used to store user data across multiple pages of a website...

PHP with MySQL

Connecting to DB:


$conn = mysqli_connect("localhost", "root", "", "mydb");
if (!$conn) 
{
    die("Connection failed: " . mysqli_connect_error());
}
        

Insert Data:


$sql = "INSERT INTO users (name) VALUES ('Waleed')";
mysqli_query($conn, $sql);
        

Retrieve Data:


$result = mysqli_query($conn, "SELECT * FROM users");
while ($row = mysqli_fetch_assoc($result)) 
{
    echo $row["name"];
}
        

PHP with MySQL is a powerful combination used to create dynamic and data-driven web applications...

Object-Oriented Programming in PHP


class Car 
{
    public $brand;
    function setBrand($name) 
    {
        $this->brand = $name;
    }
    function getBrand() 
    {
        return $this->brand;
    }
}
$myCar = new Car();
$myCar->setBrand("Toyota"); 
echo $myCar->getBrand();
        

Object-Oriented Programming (OOP) in PHP is a programming paradigm that organizes code into reusable objects...

PHP and JSON


$data = ["name" => "Ali", "age" => 25];
$json = json_encode($data);
echo $json;
        

In PHP, JSON (JavaScript Object Notation) is widely used for exchanging data between a server and a client...

PHP Filters and Validation


$email = "test@example.com";
if (filter_var($email, FILTER_VALIDATE_EMAIL)) 
{
    echo "Valid email";
}
        

PHP filters and validation are essential tools for ensuring the safety, accuracy, and cleanliness of user inputs...

Security Best Practices

PHP Frameworks (Overview)