- Introduction to PHP
- Variables in PHP
- Concatenation in PHP
- String Interpolation
- Data Types
- Control Structures
- User Input
- Arrays
- Include/Require
- Functions
- References
PHP (Hypertext Preprocessor) is a popular server-side scripting language for web development. It's easy to learn and widely used in frameworks like Laravel.
Variables in PHP are declared using the $ symbol. For example:
In PHP, there are three main types of comments, each with different syntax. Here's a breakdown of their differences:
<?php
// echo "This line is disabled.";
/*
echo "This is also disabled.";
echo "More lines are disabled.";
*/
# Mostly not Used
$name = "John";
$age = 25;
echo "My name is $name and I am $age years old.";
?>To concatenate strings, use the dot (.) operator:
<?php
$firstName = "John";
$lastName = "Doe";
echo $firstName . " " . $lastName; // Outputs: John Doe
?>String interpolation is a feature in PHP that allows you to insert (or interpolate) the value of a variable directly into a string, making it easier to write and read dynamic strings. This only works when the string is enclosed in double quotes (""), not single quotes ('').
<?php
$language = "PHP";
echo "I love $language"; // Outputs: I love PHP
echo 'I love $language'; // Outputs: I love $language
?>PHP supports a variety of data types to help developers handle different kinds of information effectively. Understanding these data types is crucial for building robust and efficient PHP applications.
Represents whole numbers (e.g., 42, -10).
Example:
$age = 25;Represents decimal numbers (e.g., 3.14, -2.5).
Example:
$price = 99.99;Represents a sequence of characters (e.g., "Hello World").
Example:
$greeting = "Hello, PHP!";Represents two possible states: true or false.
Example:
$is_logged_in = true;Represents a collection of values, indexed or associative.
Example:
$fruits = ["Apple", "Banana", "Cherry"];Represents instances of classes containing both data and methods.
Example:
class Car {
public $make;
public $model;
public function __construct($make, $model) {
$this->make = $make;
$this->model = $model;
}
}
$myCar = new Car("Toyota", "Corolla");Represents a variable with no value.
Example:
$value = null;Represents external resources like database connections or file handles.
Example:
$file = fopen("example.txt", "r");PHP dynamically converts between data types as needed, but you can also explicitly cast types.
Example of casting:
$number = (int) "42";They are used to control the flow of the Program
Executes code if a condition is true.
Example:
if ($age > 18) {
echo "You are an adult.";
}Executes one block of code if the condition is true, another if it is false.
Example:
if ($age > 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}Allows multiple conditions to be tested.
Example:
if ($score > 90) {
echo "A grade";
} elseif ($score > 75) {
echo "B grade";
} else {
echo "C grade";
}Selects one block of code to execute from many options.
Example:
$day = "Monday";
switch ($day) {
case "Monday":
echo "Start of the work week.";
break;
case "Friday":
echo "End of the work week.";
break;
default:
echo "Midweek days.";
}Introduced in PHP 8.0, match is a more powerful and concise alternative to switch. It uses strict comparisons (===), returns values directly, and does not require break statements.
$status = 200;
$message = match ($status) {
200 => 'OK',
404 => 'Not Found',
500 => 'Internal Server Error',
default => 'Unknown Status',
};
echo $message; // Output: OKExecutes a block of code as long as the condition is true.
Example:
$i = 1;
while ($i <= 5) {
echo $i;
$i++;
}Executes a block of code a specific number of times.
Example:
for ($i = 1; $i <= 5; $i++) {
echo $i;
}Executes a block of code at least once, then repeats as long as the condition is true.
$i = 1;
do {
echo $i;
$i++;
} while ($i <= 5);Iterates over each element in an array.
$fruits = ["Apple", "Banana", "Cherry"];
foreach ($fruits as $fruit) {
echo $fruit;
}- Exits the current loop or switch.
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
break;
}
echo $i;
}- Skips the current iteration of a loop and moves to the next.
for ($i = 1; $i <= 5; $i++) {
if ($i == 3) {
continue;
}
echo $i;
}The fgets(STDIN) function is used to read a line of input from the standard input (usually the console or terminal)
<?php
// Prompting user for input
echo "Enter your name: ";
// Reading input from the user
$name = fgets(STDIN);
// Removing any trailing newline or extra spaces
$name = trim($name);
echo "Hello, $name!";
?>- An indexed array in PHP is a type of array where values are stored using numerical indices (starting from 0 by default). These arrays are ideal for storing a collection of similar data where the order matters and keys are not required.
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Output: AppleAn associative array in PHP is a type of array that uses named keys (rather than numeric indices) to store and access values. These are particularly useful when you want to store data in key-value pairs, making it easier to work with structured data.
$student = [
"name" => "John Doe",
"age" => 20,
"major" => "Computer Science"
];
echo $student["name"]; // Output: John DoeIn PHP, require and include are used to include files into your PHP scripts. They allow you to reuse code, such as headers, footers, database connections, or shared functions, across multiple files. Here's a detailed explanation of both:
The include statement includes and evaluates the specified file. If the file is not found, it throws a warning but the script continues to execute.
Syntax:
include 'filename.php';Use Cases:
Ideal when the file being included is optional.
Use when the rest of the script can work even if the file is missing.
Behavior:
Generates a warning (E_WARNING) if the file is missing but does not stop the script execution.
Example:
include 'header.php'; // Includes the header file
echo "Welcome to my website!"; // This will still execute even if header.php is missing.The require statement includes and evaluates the specified file. If the file is not found, it throws a fatal error and stops the script execution.
Syntax:
require 'filename.php';Use Cases:
Ideal when the file being included is critical for the application to function.
Use when the script cannot continue without the file.
Behavior:
Generates a fatal error (E_COMPILE_ERROR) if the file is missing, halting script execution.
Example:
require 'config.php'; // Includes the configuration file
echo "This will not execute if config.php is missing.";Functions are blocks of reusable code that perform specific tasks. PHP functions can accept parameters and return values. They can be classified into built-in functions and user-defined functions.
function greet($name) {
return "Howdy, $name!\n";
}echo greet("Robert");- PHP provides a wide range of built-in functions, such as:
strlen(): Get the length of a string.
array_merge(): Merge arrays.
date(): Get the current date/time.- These are custom functions created by developers to perform specific tasks.
function greet($name) {
return "Hello, $name!";
}
echo greet("John"); // Output: Hello, John!- Functions can have default values for parameters.
function greet($name = "Guest") {
return "Hello, $name!";
}
echo greet(); // Output: Hello, Guest!
PHP allows specifying the types of function parameters and return values to ensure type safety.
function add(int $a, int $b): int {
return $a + $b;
}
echo add(2, 3); // Output: 5function getGreeting(): string {
return "Hello, World!";
}
echo getGreeting(); // Output: Hello, World!Enforce strict type checking by declaring strict types at the top of the file.
declare(strict_types=1);
function multiply(int $a, int $b): int {
return $a * $b;
}
echo multiply(3, "4"); // Error in strict mode- 💡 Note: Strict mode applies only to function/method arguments and return types. Operations like arithmetic or comparisons are not affected by strict mode:
<?php
declare(strict_types=1);
$result = "5" + 10; // Allowed: PHP still coerces string to int in operations
echo $result; // Outputs: 15Variadic functions in PHP allow a function to accept a variable number of arguments. This is useful when the number of inputs to a function isn't fixed. Starting from PHP 5.6, variadic functions are implemented using the ... operator (also known as the splat operator).
It collects multiple arguments passed to a function into an array.
function myFunction(...$args) {
// $args will be an array
}- Arguments can be of any type (int, string, object, etc.).
- The collected arguments are accessible as an indexed array.
Examples
function sum(...$numbers) {
$total = 0;
foreach ($numbers as $number) {
$total += $number;
}
return $total;
}
echo sum(1, 2, 3, 4); // Output: 10You can mix variadic arguments with regular parameters, but the variadic parameter must always come last.
function greet($greeting, ...$names) {
foreach ($names as $name) {
echo "$greeting, $name!\n" ;
}
}
greet('Hello', 'John', 'Jane', 'Doe');
// Output:
// Hello, John!
// Hello, Jane!
// Hello, Doe!The ... operator can also unpack an array into separate arguments when calling a variadic function.
function displayNames(...$names) {
foreach ($names as $name) {
echo $name."\n";
}
}
$namesArray = ['Alice', 'Bob', 'Charlie'];
displayNames(...$namesArray);
// Output:
// Alice
// Bob
// CharlieAnonymous functions, also known as closures, are functions without names. They're often used as callbacks, assigned to variables, or passed as arguments to other functions. In PHP, anonymous functions are implemented using the function keyword, and they can also capture variables from their surrounding scope.
$greet = function($name) {
return "Hello, $name!";
};
echo $greet("Ali"); // Output: Hello, Ali!Anonymous functions can be stored in variables for later use.
$multiply = function($a, $b) {
return $a * $b;
};
echo $multiply(5, 3); // Output: 15They are commonly used as arguments to functions like array_map or array_filter.
$numbers = [1, 2, 3, 4, 5];
$squared = array_map(function($n) {
return $n * $n;
}, $numbers);
print_r($squared); // Output: [1, 4, 9, 16, 25]Anonymous functions can use variables from their parent scope using the use keyword.
$factor = 3;
$multiply = function($num) use ($factor) {
return $num * $factor;
};
echo $multiply(5); // Output: 15References allow you to manipulate variables through their references rather than directly. This affects how variables are passed between functions, manipulated, and stored.
-
- When you assign or pass a variable to a function, PHP creates a copy of the variable.
- Changes to the copy do not affect the original variable.
function increment($num) {
$num++;
}
$value = 10;
increment($value);
echo $value; // Output: 10-
-
Instead of copying the value, PHP passes the variable's reference. Changes made to the parameter inside the function affect the original variable.
-
use the & symbol to pass by reference.
-
function increment(&$num) {
$num++;
}
$value = 10;
increment($value);
echo $value; // Output: 11