📌SidePHPLanguage
Introduction & Server-
✅ 1️⃣ Introduction to PHP
🌟 What is PHP?
👉 PHP stands for:
PHP: Hypertext Preprocessor
It is a widely-used, open-source, server-side scripting language designed for web
development, but it can also be used as a general-purpose language.
🌟 Key points about PHP:
Server-side scripting language → PHP code is executed on the server, and
the result (usually HTML) is sent to the browser.
Free and open source → No cost for using PHP, and its source code is
publicly available.
Cross-platform → Runs on Windows, Linux, macOS, and other OS.
Embedded in HTML → PHP code can be mixed with HTML.
Supports databases → Works with MySQL, PostgreSQL, MongoDB, SQLite,
etc.
🌟 PHP File:
A PHP file can contain:
HTML
CSS
JavaScript
📌 PHP Introduction & Server-Side Language 1
PHP code (inside <?php ... ?> tags)
File extension: .php
🌟 Example PHP code:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello, World!";
?>
</body>
</html>
👉 Output in browser:
My first PHP page
Hello, World!
📝 Explanation:
The PHP code runs on the server, and only the output ( Hello, World! ) is sent to the
browser. The actual PHP code is not visible to users.
✅ 2️⃣ What is a Server-Side Language?
🌟 Client vs. Server:
Client: The web browser (e.g., Chrome, Firefox) that sends requests to the
server and displays the response.
Server: The machine that hosts the website or web application and processes
client requests.
📌 PHP Introduction & Server-Side Language 2
🌟 What is server-side language?
A server-side language (like PHP) is used to:
✅ Process requests made by the client (browser).
✅ Interact with databases.
✅ Handle business logic (e.g., user login, form validation).
✅ Generate dynamic content (customized for each user).
✅ Securely manage data (users don’t see the actual code).
🌟 How does it work?
👉 Basic flow:
1️⃣ Browser sends request to server (e.g., open a PHP page).
2️⃣ Server executes the PHP code.
3️⃣ Server sends the resulting HTML (or other output) to the browser.
4️⃣ Browser displays the page.
👉 The user never sees the PHP code—only the result.
🌟 Example:
📌 Let’s say you have this index.php :
<?php
$name = "John";
echo "<h2>Hello, $name!</h2>";
?>
✅ What the user sees in browser:
<h2>Hello, John!</h2>
✅ What the user DOES NOT see:
📌 PHP Introduction & Server-Side Language 3
<?php
$name = "John";
echo "<h2>Hello, $name!</h2>";
?>
🌟 Why do we need a server-side language like PHP?
✅ To create dynamic pages (content that changes based on user or data).
✅ To securely process form inputs (like login forms).
✅ To connect to databases and retrieve or store data.
✅ To manage sessions and cookies (e.g., user login state).
✅ To restrict access to sensitive information (PHP code is hidden from the user).
✅ Client-side vs Server-side (Simple Comparison)
Client-side (e.g. HTML, CSS, JS) Server-side (e.g. PHP)
Runs on browser Runs on web server
Code visible to user Code hidden from user
Can modify content in browser Can create dynamic content before it reaches browser
Limited to client resources Access to server resources (database, files)
🌟 Summary
📌 PHP Introduction & Server-Side Language 4
💡 PHP is a server-side scripting language mainly used for web development.
💡 A server-side language runs on the server, processes data, and sends only
the output (usually HTML) to the browser.
💡 Why use PHP? → It's free, easy to learn, and powerful for creating dynamic
websites.
📌 PHP Introduction & Server-Side Language 5
📌 PHP Data Types, Operators, Loops, and Functions
1️⃣ PHP Data Types
In PHP, variables can hold different types of data. PHP automatically converts the
variable to the correct data type depending on its value. PHP is a loosely typed
language (you don't have to declare data types explicitly).
📌 PHP Introduction & Server-Side Language 6
Common PHP Data Types:
Type Description
Integer Whole numbers without decimal points (e.g., 10, -3)
Float (Double) Numbers with decimal points (e.g., 3.14, -7.5)
String Sequence of characters (e.g., "Hello", 'World')
Boolean Represents true or false
Array Collection of values (indexed, associative, or multidimensional)
Object Instance of a class
NULL Represents a variable with no value
Special variables holding references to external resources (e.g., database
Resource
connection)
Examples:
<?php
$integerVar = 10; // Integer
$floatVar = 3.14; // Float
$stringVar = "Hello PHP!"; // String
$boolVar = true; // Boolean
$arrayVar = array(1, 2, 3); // Array
$nullVar = NULL; // Null
echo $integerVar; // Outputs: 10
echo "<br>";
📌 PHP Introduction & Server-Side Language 7
echo $stringVar; // Outputs: Hello PHP!
?>
👉 Note: PHP automatically determines data type at runtime.
2️⃣ PHP Operators
Operators in PHP are used to perform operations on variables and values.
Arithmetic Operators
Operator Name Example Result
+ Addition 5+2 7
- Subtraction 5-2 3
* Multiplication 5*2 10
/ Division 5/2 2.5
% Modulus 5%2 1
Example:
<?php
$a = 10;
$b = 3;
echo $a + $b; // 13
echo "<br>";
echo $a % $b; // 1
?>
Assignment Operators
📌 PHP Introduction & Server-Side Language 8
Operator Example Same As
= $x = 5 $x = 5
+= $x += 3 $x = $x + 3
-= $x -= 3 $x = $x - 3
*= $x *= 3 $x = $x * 3
/= $x /= 3 $x = $x / 3
%= $x %= 3 $x = $x % 3
Example:
<?php
$x = 5;
$x += 2; // x is now 7
echo $x;
?>
Comparison Operators
Operator Description Example
== Equal $a == $b
=== Identical (equal + same type) $a === $b
!= / <> Not equal $a != $b
!== Not identical $a !== $b
> Greater than $a > $b
< Less than $a < $b
>= Greater or equal $a >= $b
<= Less or equal $a <= $b
Operator Description
&& And
📌 PHP Introduction & Server-Side Language 9
! Not
Logical Operators
Example:
<?php
$a = 5;
$b = 10;
if ($a < $b && $a != 0) {
echo "Condition is true";
}
?>
3️⃣ PHP Loops
Loops are used to execute a block of code repeatedly.
while loop
Executes as long as condition is true.
<?php
$i = 1;
while ($i <= 5) {
echo $i . " ";
$i++;
}
// Output: 1 2 3 4 5
?>
do...while loop
Similar to while, but executes the block at least once.
📌 PHP Introduction & Server-Side Language 10
<?php
$i = 1;
do {
echo $i . " ";
$i++;
} while ($i <= 5);
?>
for loop
Used when the number of iterations is known.
<?php
for ($i = 1; $i <= 5; $i++) {
echo $i . " ";
}
?>
foreach loop
Specially for arrays.
<?php
$arr = array(1, 2, 3, 4);
foreach ($arr as $value) {
echo $value . " ";
}
?>
4️⃣ PHP Functions
A function is a block of statements that can be reused.
Syntax:
📌 PHP Introduction & Server-Side Language 11
function functionName($param1, $param2 = defaultValue) {
// code to execute
return $result;
}
Example 1: Simple function
<?php
function greet() {
echo "Hello, welcome!";
}
greet();
?>
Example 2: Function with parameters
<?php
function add($a, $b) {
return $a + $b;
}
echo add(5, 3); // Outputs: 8
?>
Example 3: Function with default parameter
<?php
function sayHello($name = "Guest") {
echo "Hello, $name!";
}
sayHello(); // Hello, Guest!
sayHello("John"); // Hello, John!
?>
📌 PHP Introduction & Server-Side Language 12
Example 4: Returning value
<?php
function square($x) {
return $x * $x;
}
echo square(4); // Outputs: 16
?>
Example 5: Passing by reference
<?php
function addOne(&$number) {
$number++;
}
$num = 5;
addOne($num);
echo $num; // Outputs: 6
?>
📌 Summary
✅ PHP supports multiple data types like integers, strings, arrays, and objects.
✅ Operators perform arithmetic, comparison, logical, and assignment tasks.
✅ Loops ( , , for ,
while ) help in repeated execution.
do...while foreach
✅ Functions promote reusability and clean code with or without parameters and
return values.
📌 PHP Introduction & Server-Side Language 13