1. Home
  2. Learn
  3. Introduction to Programming in PHP

Introduction to Programming in PHP

PHP (Hypertext Preprocessor) is a widely-used server-side scripting language designed for web development. It is embedded in HTML and is particularly suited for creating dynamic web pages, handling forms, and interacting with databases. PHP powers many popular content management systems (CMS) like WordPress and frameworks like Laravel.

This guide will introduce you to the fundamentals of PHP programming, helping you build dynamic and interactive web applications.


Table of Contents

  1. What is PHP?
  2. Setting Up Your Environment
  3. Variables and Data Types
  4. Control Flow
  5. Functions
  6. Arrays
  7. Forms and User Input
  8. Working with Databases
  9. Sessions and Cookies
  10. Object-Oriented Programming

What is PHP?

PHP is a server-side scripting language that is executed on the server, generating HTML which is then sent to the client's browser. It is especially suited for web development and can be embedded into HTML.

1// Example: Your first PHP program
2<?php
3    echo "Hello, PHP Programming!";
4?>

Setting Up Your Environment

To write and run PHP programs, you need a local server environment like XAMPP, WAMP, or MAMP. Alternatively, you can use a web server with PHP installed.

1# Example: Running a PHP script
2php -S localhost:8000

Variables and Data Types

PHP is loosely typed, meaning you don't need to declare variable types explicitly. Common data types include integer, float, string, boolean, and array.

1// Example: Declaring variables
2<?php
3    $age = 25;
4    $height = 5.9;
5    $name = "Alice";
6    $isStudent = true;
7
8    echo "Name: $name, Age: $age, Height: $height";
9?>

Control Flow

PHP supports if, else, switch, and loops (for, while, do-while) for controlling program flow.

 1// Example: Conditional statement
 2<?php
 3    $age = 18;
 4
 5    if ($age >= 18) {
 6        echo "You are an adult.";
 7    } else {
 8        echo "You are a minor.";
 9    }
10?>

Functions

Functions are reusable blocks of code that perform a specific task. PHP supports both built-in and user-defined functions.

1// Example: Function
2<?php
3    function add($a, $b) {
4        return $a + $b;
5    }
6
7    $result = add(5, 10);
8    echo "Result: $result"; // 15
9?>

Arrays

Arrays in PHP can store multiple values in a single variable. PHP supports indexed arrays, associative arrays, and multidimensional arrays.

1// Example: Indexed array
2<?php
3    $fruits = array("Apple", "Banana", "Orange");
4
5    foreach ($fruits as $fruit) {
6        echo "$fruit\n";
7    }
8?>

Forms and User Input

PHP is commonly used to handle form data submitted by users. The $_GET and $_POST superglobals are used to collect form data.

 1// Example: Handling form data
 2<?php
 3    if ($_SERVER["REQUEST_METHOD"] == "POST") {
 4        $name = $_POST['name'];
 5        echo "Hello, $name!";
 6    }
 7?>
 8
 9<form method="post" action="">
10    Name: <input type="text" name="name">
11    <input type="submit">
12</form>

Working with Databases

PHP can interact with databases like MySQL to store and retrieve data. The mysqli extension is commonly used for this purpose.

 1// Example: Connecting to a MySQL database
 2<?php
 3    $servername = "localhost";
 4    $username = "root";
 5    $password = "";
 6    $dbname = "myDB";
 7
 8    // Create connection
 9    $conn = new mysqli($servername, $username, $password, $dbname);
10
11    // Check connection
12    if ($conn->connect_error) {
13        die("Connection failed: " . $conn->connect_error);
14    }
15    echo "Connected successfully";
16?>

Sessions and Cookies

Sessions and cookies are used to store user data across multiple pages. Sessions are stored on the server, while cookies are stored on the client's browser.

1// Example: Using sessions
2<?php
3    session_start();
4    $_SESSION["username"] = "Alice";
5    echo "Session username is " . $_SESSION["username"];
6?>

Object-Oriented Programming

PHP supports object-oriented programming (OOP) with classes, objects, inheritance, and more.

 1// Example: Class and Object
 2<?php
 3    class Person {
 4        public $name;
 5        public $age;
 6
 7        function __construct($name, $age) {
 8            $this->name = $name;
 9            $this->age = $age;
10        }
11
12        function display() {
13            echo "Name: $this->name, Age: $this->age";
14        }
15    }
16
17    $person = new Person("Alice", 25);
18    $person->display();
19?>