PHP Guide
PHP Guide
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,
A PHP script is executed on the server, and the plain HTML result is sent to the browser. PHP
Example:
<?php
?>
Variables in PHP start with a $ sign, followed by the name of the variable. PHP supports different
Example:
<?php
$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: +, -, *, /, %
- Comparison Operators: ==, ===, !=, !==, >, <, >=, <=
Example:
<?php
$a = 10;
$b = 5;
echo $a + $b; // 15
?>
Control structures in PHP include conditional statements and loops that control the flow of
execution.
Example:
<?php
} else {
?>
PHP Guide
Example:
<?php
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) {
?>
Chapter 7: Arrays
Arrays are used to store multiple values in a single variable. PHP supports indexed arrays,
Example:
<?php
PHP Guide
$person = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
?>
Chapter 8: Superglobals
PHP superglobals are built-in variables that are always accessible, regardless of scope. Common
superglobals include:
Example:
<?php
echo $_SERVER['PHP_SELF'];
echo $_POST['name'];
?>
Forms are used to collect user input. PHP can collect form data sent via both the GET and POST
methods.
Example:
<input type='submit'>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = $_POST['name'];
echo $name;
?>
PHP can connect to various databases, the most common being MySQL. You can use the mysqli or
<?php
if ($conn->connect_error) {
$conn->close();
?>
Sessions and cookies are used to store information across different pages. Sessions are stored on
PHP Guide
<?php
session_start();
$_SESSION['username'] = 'John';
echo $_SESSION['username'];
?>
<?php
echo $_COOKIE['user'];
?>
PHP provides functions for file handling, allowing you to read, write, and manipulate files. Common
Example:
<?php
fclose($file);
?>