PHP Concept(Introduction)
PHP Concept(Introduction)
1. Introduction to PHP
PHP (Hypertext Preprocessor) is a server-side scripting language primarily used for web
development. PHP code is executed on the server, and the result is sent to the browser.
✅ Example:
<?php
echo "Hello, World!";
?>
PHP is loosely typed, but sometimes you need to convert between types explicitly.
✅ Example:
🧠 Output: 15
🔹 3. Operators
Arithmetic: +, -, *, /
Assignment: =, +=
Comparison: ==, ===, !=
Logical: &&, ||, !
✅ Example:
$a = 10;
$b = 5;
echo $a + $b; // Arithmetic
echo $a > $b; // Comparison
🔹 4. Arrays
Indexed Arrays
Associative Arrays
Multidimensional Arrays
✅ Example:
// Indexed
$colors = ["Red", "Green", "Blue"];
echo $colors[1]; // Green
// Associative
$ages = ["John" => 25, "Jane" => 22];
echo $ages["Jane"]; // 22
🔹 5. String Comparison
✅ Example:
$str1 = "hello";
$str2 = "Hello";
if (strcmp($str1, $str2) == 0) {
echo "Equal";
} else {
echo "Not equal";
}
🔹 6. Regular Expression
Used for pattern matching and validation using functions like preg_match().
✅ Example:
= "[email protected]";
if (preg_match("/^[a-z0-9._%-]+@[a-z0-9.-]+\.[a-z]{2,4}$/", $email)) {
echo "Valid email";
} else {
echo "Invalid email";
}
✅ Example (HTML):
✅ process.php:
<?php
$name = $_POST['name'];
echo "Hello, " . htmlspecialchars($name);
?>
✅ Example:
while($row = mysqli_fetch_assoc($result)) {
echo $row['username'] . "<br>";
}
mysqli_close($conn);
🔹 9. Using Cookies
✅ Set a Cookie:
✅ Read a Cookie:
if(isset($_COOKIE["user"])) {
echo "User is " . $_COOKIE["user"];
}
🔹 10. AJAX Web Application
AJAX (Asynchronous JavaScript and XML) allows updating parts of a page without
reloading.
✅ HTML:
✅ JavaScript:
function loadData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.php", true);
xhr.onload = function() {
if (this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
}
};
xhr.send();
}
✅ data.php:
<?php
echo "This data was loaded via AJAX!";
?>
1. Introduction to PHP
PHP (Hypertext Preprocessor) is a server-side scripting language widely used for web
development. It is embedded within HTML and executes on the server.
Example:
<?php
echo "Hello, World!";
?>
Explanation: This script uses echo to output the string "Hello, World!". The <?php ... ?>
tags denote PHP code.
Example:
$number = "10";
$converted = (int)$number;
echo $converted + 5; // Outputs 15
Explanation: The string "10" is typecast to an integer using (int), and then added to 5. The
result is 15.
3. Operators in PHP
Types of operators:
Arithmetic: +, -, *, /
Assignment: =, +=, etc.
Comparison: ==, ===, !=
Logical: &&, ||, !
Example:
$a = 10;
$b = 5;
echo $a + $b; // Outputs 15
echo $a > $b; // Outputs 1 (true)
Explanation: The first line adds two integers. The second line compares them and returns
true (represented as 1 in PHP).
4. Arrays in PHP
Indexed Arrays
Associative Arrays
Multidimensional Arrays
Example:
Explanation: $colors is an indexed array where elements are accessed using numeric
indices. $ages is an associative array where elements are accessed using named keys.
5. String Comparison
Example:
$str1 = "hello";
$str2 = "Hello";
echo strcmp($str1, $str2); // Outputs a non-zero value (case-sensitive)
Explanation: strcmp() compares two strings. It returns 0 if they are equal, a positive
number if $str1 is greater, or a negative if smaller. Case matters here.
6. Regular Expressions
Example:
$email = "[email protected]";
if (preg_match("/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/", $email)) {
echo "Valid email";
} else {
echo "Invalid email";
}
Explanation: The preg_match function checks if $email matches the given regular
expression pattern for a basic email format.
HTML Form:
process.php:
<?php
$name = $_POST['name'];
echo "Hello, " . htmlspecialchars($name);
?>
Explanation: The form sends data via POST to process.php. The PHP script retrieves the
name using $_POST['name'] and prints a greeting, using htmlspecialchars() to prevent
XSS attacks.
Example:
while($row = mysqli_fetch_assoc($result)) {
echo $row['username'] . "<br>";
}
mysqli_close($conn);
Explanation:
9. Using Cookies
Set a Cookie:
Read a Cookie:
if(isset($_COOKIE["user"])) {
echo "User is " . $_COOKIE["user"];
}
Explanation: setcookie() sets a cookie named "user" with value "John" that expires in 1
hour. $_COOKIE is used to access the stored cookie value.
HTML:
JavaScript:
function loadData() {
var xhr = new XMLHttpRequest();
xhr.open("GET", "data.php", true);
xhr.onload = function() {
if (this.status == 200) {
document.getElementById("result").innerHTML = this.responseText;
}
};
xhr.send();
}
data.php:
<?php
echo "This data was loaded via AJAX!";
?>
Explanation: This PHP file simply returns a string. When requested via AJAX, the string is
inserted into the web page without reloading.