Introduction to PHP
• PHP (Hypertext Preprocessor) is a server-side
scripting language used for web development.
• - Runs on the server
• - Embedded in HTML
• - Supports databases
Basic PHP Syntax
PHP code is written inside <?php ... ?> tags.
Example:
<?php
echo 'Hello, World!';
?>
PHP Variables
• A variable in PHP is the name of the memory
location that holds data. In PHP, a variable is
declared using the $ sign followed by the variable
name. The main way to store information in the
middle of a PHP program is by using a variable.
Example:
$name = 'John';
$age = 25;
echo $name;
exercise
<?php
$name = "John";
$age = 25;
$price = 99.99;
echo "My name is $name and I am $age years old.";
?>
Exercise 2
<!DOCTYPE html>
<html>
<body>
<?php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?>
</body>
</html>
Exercise 3
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5;
$y = 4;
echo $x + $y;
?>
</body>
</html>
Data Types in PHP
PHP supports:
- Strings ('Hello')
- Integers (10)
- Floats (3.14)
- Booleans (true/false)
- Arrays, Objects, NULL
String
A string is a sequence of characters, like "Hello world!".
A string can be any text inside quotes. You can use single or double quotes:
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello world!";
$y = 'Hello world!';
var_dump($x);
echo "<br>";
var_dump($y);
?>
</body>
</html>
PHP Integer
An integer data type is a non-decimal number between -
2,147,483,648 and 2,147,483,647.
Rules for integers:
•An integer must have at least one digit
•An integer must not have a decimal point
•An integer can be either positive or negative
•Integers can be specified in: decimal (base 10),
hexadecimal (base 16), octal (base 8), or binary (base 2)
notation
In the following example $x is an integer. The
PHP var_dump() function returns the data type and value:
Example
<!DOCTYPE html>
<html>
<body>
<?php
$x = 5985;
var_dump($x);
?>
</body>
</html>
PHP Float
A float (floating point number) is a number with a decimal point or
a number in exponential form.
In the following example $x is a float. The
PHP var_dump() function returns the data type and value:
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10.365;
var_dump($x);
?>
</body>
</html>
PHP Boolean
• A Boolean represents two possible states: TRUE or FAL
<!DOCTYPE html>
<html>
<body>
<?php
$x = true;
var_dump($x);
?>
</body>
</html>SE
PHP Array
An array stores multiple values in one single variable.
In the following example $cars is an array. The
PHP var_dump() function returns the data type and value:
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("Volvo","BMW","Toyota");
var_dump($cars);
?>
</body>
</html>
PHP NULL Value
• Null is a special data type which can have only one value: NULL.
• A variable of data type NULL is a variable that has no value assigned to it.
• Tip: If a variable is created without a value, it is automatically assigned a value of
NULL.
• Variables can also be emptied by setting the value to NULL:
<!DOCTYPE html>
<html>
<body>
<?php
$x = "Hello world!";
$x = null;
var_dump($x);
?>
</body>
</html>
Operators in PHP
Operators are used to perform operations on
variables and values.
PHP divides the operators in the following groups:
– Arithmetic operators
– Assignment operators
– Comparison operators
– Increment/Decrement operators
– Logical operators
– String operators
– Array operators
– Conditional assignment operators
Arithmetic operators
Example 1
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x + $y;
?>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x - $y;
?>
</body>
</html>
Example 3
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x*$y;
?>
</body>
</html>
Example 4
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 6;
echo $x/$y;
?>
</body>
</html>
Example 4
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
$y = 6;
echo $x*$y;
?>
</body>
</html>
Example 5
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 6;
echo $x%$y;
?>
</body>
</html>
Example 6
<!DOCTYPE html>
<html>
<body>
<?php
$x = 50;
$y = 6;
echo $x**$y;
?>
</body>
</html>
PHP Assignment Operators
• The PHP assignment operators are used with numeric values
to write a value to a variable.
• The basic assignment operator in PHP is "=". It means that the
left operand gets set to the value of the assignment
expression on the right
Example 1
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x;
?>
</body>
</html>
Comparison operators
Example
<!DOCTYPE html>
<html>
<body>
<?php
$x = 100;
$y = "100";
var_dump($x == $y); // returns true because values are equal
?>
</body>
</html>
Php increment/decrement
Example 1
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo ++$x;
?>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
echo $x++;
?>
</body>
</html>
Logical operator
Example 1
<!DOCTYPE html>
<html>
<body>
<h1>The and Operator</h1>
<p>Write a message if both conditions are true.</p>
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
Example 2
<!DOCTYPE html>
<html>
<body>
<h1>The and Operator</h1>
<p>Write a message if both conditions are true.</p>
<?php
$x = 100;
$y = 50;
if ($x == 100 and $y == 50) {
echo "Hello world!";
}
?>
</body>
</html>
Control Structures
• Conditional statements:
• if ($age >= 18) { echo 'Adult'; } else { echo
'Minor'; }
• Loops:
• for, while, do-while, foreach
PHP Functions
• Functions help reuse code.
• Example:
• function greet($name) {
• return 'Hello, ' . $name;
• }
• echo greet('John');
Arrays in PHP
• Types:
• - Indexed: $colors = ['Red', 'Green', 'Blue'];
• - Associative: $person = ['name' => 'John',
'age' => 25];
• - Multidimensional
Super Global Variables
• Common ones:
• - $_GET (URL parameters)
• - $_POST (Form data)
• - $_SESSION (Session storage)
• - $_COOKIE (Small user data)
Connecting PHP with MySQL
• 1. Connect to DB:
• $conn = mysqli_connect('localhost', 'root', '', 'test_db');
• 2. Insert Data:
• $sql = "INSERT INTO users (name, email) VALUES
('John', '[email protected]')";
• 3. Fetch Data:
• $result = mysqli_query($conn, 'SELECT * FROM users');