PHP-OVERVIEW

Objectives: PHP-OVERVIEW-SUMMARY

PHP Full Notes

PHP Full Notes - Theory & Practical

1. Introduction to PHP

PHP (Hypertext Preprocessor) is a server-side scripting language used to create dynamic web pages. It is embedded within HTML and executed on the server.

  • Created by Rasmus Lerdorf in 1994
  • Open-source and platform-independent
  • Files have the extension .php

2. PHP Syntax

PHP code is written inside <?php ... ?> tags. Example:

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

3. Variables & Data Types

  • Variables start with $, e.g., $name
  • Data types include: String, Integer, Float, Boolean, Array, Object, NULL
$name = "John";
$age = 25;
$isStudent = true;

4. Control Structures

If/Else:
if ($age >= 18) {
    echo "Adult";
} else {
    echo "Minor";
}
Switch:
switch ($day) {
    case "Monday":
        echo "Start of week";
        break;
    default:
        echo "Other day";
}
Loops:
// For Loop
for ($i = 0; $i < 5; $i++) {
    echo $i;
}

// While Loop
$i = 0;
while ($i < 5) {
    echo $i;
    $i++;
}

5. Functions

Functions are blocks of code designed to perform a specific task.

function greet($name) {
    return "Hello, $name!";
}

echo greet("Alice");

6. Arrays

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

// Associative Array
$user = ["name" => "John", "age" => 25];
echo $user["name"];

7. Superglobals

  • $_GET, $_POST - form data
  • $_SERVER - server info
  • $_SESSION - session data
  • $_COOKIE - cookie data

8. Forms

<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    echo "Hello, " . htmlspecialchars($_POST['name']);
}
?>

9. File Handling

// Writing to a file
file_put_contents("file.txt", "Hello File");

// Reading from a file
$content = file_get_contents("file.txt");
echo $content;

10. Database (MySQLi)

// Connect to database
$conn = new mysqli("localhost", "root", "", "test");

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Insert data
$sql = "INSERT INTO users (name) VALUES ('John')";
$conn->query($sql);

// Fetch data
$result = $conn->query("SELECT * FROM users");
while($row = $result->fetch_assoc()) {
    echo $row['name'];
}

$conn->close();

11. OOP in PHP

class Person {
    public $name;

    function __construct($name) {
        $this->name = $name;
    }

    function greet() {
        return "Hi, I am $this->name";
    }
}

$p = new Person("Alice");
echo $p->greet();

12. Security Tips

  • Use htmlspecialchars() to prevent XSS
  • Validate all input data
  • Use prepared statements to prevent SQL Injection
  • Never expose credentials

13. Hosting & Deployment

Use platforms like XAMPP, LAMP, or online hosts (e.g. InfinityFree, 000webhost). Ensure:

  • PHP files are in the htdocs directory
  • Apache and MySQL services are running
  • Access with http://localhost/project

14. Final Words

Mastering PHP requires practice. Focus on small projects, read code, and experiment with real applications. Combine PHP with HTML, JavaScript, and SQL for full-stack development.

Reference Book: N/A

Author name: SIR H.A.Mwala Work email: biasharaboraofficials@gmail.com
#MWALA_LEARN Powered by MwalaJS #https://mwalajs.biasharabora.com
#https://educenter.biasharabora.com

:: 1.2::