What is PHP?
PHP (Hypertext Preprocessor) is a widely-used server-side scripting language designed for web development but also used as a general-purpose programming language.
Basic Syntax
A PHP script starts with <?php
and ends with ?>
:
<?php
echo "Hello, World!";
?>
Variables and Data Types
PHP variables are declared with the $
sign:
<?php
$name = "John";
$age = 25;
$price = 19.99;
$isAvailable = true;
?>
Data Types in PHP
PHP supports several data types:
- String - A sequence of characters
$text = "Hello, PHP!";
echo $text;
- Integer - Whole numbers
$num = 100;
echo $num;
- Float (Double) - Decimal numbers
$price = 99.99;
echo $price;
- Boolean - True or False values
$is_valid = true;
echo $is_valid;
- Array - Stores multiple values
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0];
- Object - Stores custom objects
class Car {
public $brand;
}
$myCar = new Car();
$myCar->brand = "Toyota";
echo $myCar->brand;
- NULL - Represents a variable with no value
$x = NULL;
- Resource - Special type for handling file or database connections
$file = fopen("test.txt", "r");
Control Structures
Conditional Statements
if ($age >= 18) {
echo "Adult";
} else {
echo "Minor";
}
Loops
While Loop
$i = 1;
while ($i <= 5) {
echo $i . " ";
$i++;
}
For Loop
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
Functions
Functions in PHP are declared using the function
keyword:
function greet($name) {
return "Hello, " . $name;
}
echo greet("Alice");
Arrays
Indexed Array
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0];
Associative Array
$person = ["name" => "Alice", "age" => 30];
echo $person["name"];
Multidimensional Array
$employees = [
["name" => "John", "age" => 28],
["name" => "Jane", "age" => 32]
];
echo $employees[0]["name"];
Object-Oriented Programming (OOP)
Class and Object
class Car {
public $brand;
public function setBrand($brand) {
$this->brand = $brand;
}
public function getBrand() {
return $this->brand;
}
}
$myCar = new Car();
$myCar->setBrand("Toyota");
echo $myCar->getBrand();
Inheritance
class ElectricCar extends Car {
public $batteryCapacity;
public function setBattery($capacity) {
$this->batteryCapacity = $capacity;
}
public function getBattery() {
return $this->batteryCapacity;
}
}
$tesla = new ElectricCar();
$tesla->setBrand("Tesla");
$tesla->setBattery("100 kWh");
echo $tesla->getBrand() . " - " . $tesla->getBattery();
PHP and MySQL
Connecting to a Database
$conn = new mysqli("localhost", "username", "password", "database");
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Fetching Data
$result = $conn->query("SELECT * FROM users");
while ($row = $result->fetch_assoc()) {
echo $row["name"] . "<br>";
}
Error Handling
Using try-catch
try {
$conn = new PDO("mysql:host=localhost;dbname=myDB", "username", "password");
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
Best Practices
- Use Prepared Statements to prevent SQL injection.
- Follow the MVC pattern for structured code.
- Enable error reporting during development using
error_reporting(E_ALL);
. - Secure sensitive data with hashing (
password_hash()
). - Use
require
andinclude
for reusable components. - Follow PSR coding standards for clean, maintainable code.
This guide provides a complete overview of PHP fundamentals, covering syntax, control structures, OOP, and database interactions.