PHP Guide
Chapter 1: Introduction to PHP
PHP (Hypertext Preprocessor) is a widely-used open source scripting language that is especially
suited for web development and can be embedded into HTML. It is executed on the server,
generating HTML which is then sent to the client.
Chapter 2: PHP Syntax
A PHP script is executed on the server, and the plain HTML result is sent to the browser. PHP
scripts are enclosed in <?php and ?> tags.
Example:
<?php
echo 'Hello, World!';
?>
Chapter 3: Variables and Data Types
Variables in PHP start with a $ sign, followed by the name of the variable. PHP supports different
data types such as strings, integers, floats, arrays, and objects.
Example:
<?php
$text = 'Hello, World!';
$number = 42;
$float = 3.14;
?>
Chapter 4: Operators
PHP Guide
PHP operators are used to perform operations on variables and values. Common operators include:
- Arithmetic Operators: +, -, *, /, %
- Assignment Operators: =, +=, -=, *=, /=
- Comparison Operators: ==, ===, !=, !==, >, <, >=, <=
- Logical Operators: &&, ||, !
Example:
<?php
$a = 10;
$b = 5;
echo $a + $b; // 15
echo $a > $b; // true
?>
Chapter 5: Control Structures
Control structures in PHP include conditional statements and loops that control the flow of
execution.
- Conditional Statements: if, else if, else, switch
Example:
<?php
if ($x > 10) {
echo 'x is greater than 10';
} else {
echo 'x is less than or equal to 10';
?>
PHP Guide
- Loops: for, while, do...while, foreach
Example:
<?php
for ($i = 0; $i < 5; $i++) {
echo $i;
?>
Chapter 6: Functions
Functions are blocks of code designed to perform a particular task. They are executed when they
are called. You can define a function using the function keyword.
Example:
<?php
function greet($name) {
return 'Hello, ' . $name . '!';
echo greet('John'); // Hello, John!
?>
Chapter 7: Arrays
Arrays are used to store multiple values in a single variable. PHP supports indexed arrays,
associative arrays, and multidimensional arrays.
Example:
<?php
PHP Guide
$fruits = ['Apple', 'Banana', 'Orange'];
$person = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
echo $fruits[0]; // Apple
echo $person['name']; // John
?>
Chapter 8: Superglobals
PHP superglobals are built-in variables that are always accessible, regardless of scope. Common
superglobals include:
- $_GET: Collects data sent in the URL
- $_POST: Collects data sent in the HTTP POST method
- $_SERVER: Holds information about headers, paths, and script locations
Example:
<?php
echo $_SERVER['PHP_SELF'];
echo $_POST['name'];
?>
Chapter 9: Working with Forms
Forms are used to collect user input. PHP can collect form data sent via both the GET and POST
methods.
Example:
<form method='post' action='<?php echo $_SERVER['PHP_SELF']; ?>'>
Name: <input type='text' name='name'>
PHP Guide
<input type='submit'>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
echo $name;
?>
Chapter 10: Connecting to a Database
PHP can connect to various databases, the most common being MySQL. You can use the mysqli or
PDO extension to connect to a MySQL database.
Example using mysqli:
<?php
$conn = new mysqli('localhost', 'username', 'password', 'database');
if ($conn->connect_error) {
die('Connection failed: ' . $conn->connect_error);
echo 'Connected successfully';
$conn->close();
?>
Chapter 11: Sessions and Cookies
Sessions and cookies are used to store information across different pages. Sessions are stored on
PHP Guide
the server, while cookies are stored on the client's browser.
Example of using sessions:
<?php
session_start();
$_SESSION['username'] = 'John';
echo $_SESSION['username'];
?>
Example of using cookies:
<?php
setcookie('user', 'John', time() + (86400 * 30), '/');
echo $_COOKIE['user'];
?>
Chapter 12: File Handling
PHP provides functions for file handling, allowing you to read, write, and manipulate files. Common
functions include fopen(), fread(), fwrite(), and fclose().
Example:
<?php
$file = fopen('test.txt', 'w');
fwrite($file, 'Hello, World!');
fclose($file);
?>