Introduction

PHP is a widely-used, open-source, server-side scripting language. It is especially suited for web development and can be embedded into HTML.

OOP Concepts in PHP

Object-Oriented Programming (OOP) in PHP enables developers to organize code into reusable classes and objects. Key concepts include:

  • Encapsulation: Restricting access to certain parts of an object.
  • Inheritance: A class can inherit properties and methods from another class.
  • Polymorphism: A method can have different behaviors based on the object that invokes it.
  • Abstraction: Defining abstract methods in a class, implemented by child classes.

class Car {
    public $color;
    function __construct($color) {
        $this->color = $color;
    }
}
            

Data Types in PHP

PHP supports several data types, including:

  • String: A sequence of characters.
  • Integer: Non-decimal numbers.
  • Float: Decimal numbers.
  • Boolean: Either true or false.
  • Array: A collection of values.
  • Object: An instance of a class.
  • NULL: Represents a variable with no value.

Variables and Constants

Variables in PHP are declared with a $ sign and can hold various data types. Constants are immutable and defined using the define() function.


$var = "Hello World!";
define("PI", 3.14);
            

Control Structures (Conditions and Loops)

PHP offers several control structures to manage the flow of the program:

  • If/Else Statements: Conditional logic to execute code based on certain conditions.
  • Switch Statements: Similar to if but for multiple conditions.
  • Loops:
    • for loop
    • while loop
    • foreach loop for arrays
    • do...while loop

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

Functions in PHP

Functions in PHP are reusable blocks of code that perform specific tasks. You can define custom functions or use built-in PHP functions.


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

Superglobals

PHP superglobals are predefined variables that are accessible globally throughout the script. Common superglobals include:

  • $_GET: Retrieve data sent via HTTP GET.
  • $_POST: Retrieve data sent via HTTP POST.
  • $_SESSION: Store user session data.
  • $_COOKIE: Store small pieces of data in the user's browser.

Error Handling in PHP

PHP provides various ways to handle errors and exceptions:

  • Try-Catch Blocks: Handle exceptions in PHP code.
  • Custom Error Handlers: Define custom error handling using set_error_handler().
  • Error Reporting: Adjust the level of error reporting using error_reporting().

Connecting PHP with Databases (MySQL)

PHP can interact with MySQL databases using:

  • MySQLi: Improved MySQL extension for interacting with MySQL databases.
  • PDO (PHP Data Objects): A unified API for different database systems.

$conn = new mysqli("localhost", "username", "password", "database");
            

Sessions and Cookies in PHP

Sessions store user information across multiple pages, while cookies store small pieces of data in the user's browser.


session_start();
$_SESSION["username"] = "Shankar";
            

File Handling in PHP

PHP allows you to handle files through reading, writing, and uploading:


$file = fopen("testfile.txt", "r");
            

Security Best Practices in PHP

Ensure security by following these best practices:

  • SQL Injection Prevention: Use prepared statements to avoid SQL injection.
  • Cross-Site Scripting (XSS) Prevention: Sanitize user input using htmlspecialchars().
  • CSRF Protection: Use CSRF tokens for forms to prevent cross-site request forgery.

Working with APIs in PHP

PHP can work with external APIs using curl or the file_get_contents() function.


$json = file_get_contents("https://api.example.com/data");
$data = json_decode($json);