06 PHP Introduction
06 PHP Introduction
2024-04-08
Lecturer
Dr. HAMDANI M
"[php]": {
"editor.defaultFormatter":
"bmewburn.vscode-intelephense-client"
<?php
?>
A simple .php file with both HTML code and PHP code:
<!DOCTYPE html>
<html>
<body>
<h1>My first PHP page</h1>
<?php
echo "Hello World!";
?>
</body>
</html>
Single-line comments: //
Multi-line comments: /* */
<?php
// Single-line comment
/*
Multi-line
comment
*/
?>
<?php
$name = "Ahmed";
$age = 12;
?>
Echo:
<?php
echo "Hello, world!"; // no parentheses required
print("Hello, world!"); // parentheses required
?>
<?php
$integer = 42;
$float = 3.14;
$boolean = true;
<?php
$greeting = "Hello";
$name = "Ahmed";
?>
Variables within double quotes are parsed and their values are inserted
into the string.
Escape sequences like \n, \t, etc., are interpreted as newline, tab, etc
$name = "Ahmed";
echo 'Hello, $name!'; // Output: Hello, $name!
?: Ternary $x = expr1 ? expr2 : expr3 Returns the value of $x. The value
of $x is expr2 if expr1 is TRUE.
The value of $x is expr3 if expr1 is
FALSE.
?? Null coalescing $x = expr1 ?? expr2 Returns the value of $x. The value
of $x is expr1 if expr1 exists and is
not NULL. If expr1 does not exist or
is NULL, the value of $x is expr2.
Introduced in PHP 7.
<?php
if (condition) {
// Code to execute if condition is true
} elseif (another_condition) {
// Code to execute if another_condition is true
} else {
// Code to execute if none of the above conditions are
true
}
?>
<?php
while (condition) {
// Code to execute while condition is true
}
?>
Do-While Loop
<?php
do {
//Code to execute at least once, then while condition is true
} while (condition);
?>
<?php
for ($i = 0; $i < count($array); $i++) {
// Code to execute for each iteration
}
?>
Foreach Loop
<?php
foreach ($array as $key => $value) {
// Code to execute for each element in the array
}
<?php
// Define an array of student names
$students = array("John", "Alice", "Bob", "Emily");
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i == 5) {
break; // Exit the loop when $i equals 5
}
echo $i . " ";
}
//output : 1 2 3 4
?>
<?php
for ($i = 1; $i <= 10; $i++) {
if ($i % 2 == 0) {
continue; // Skip even numbers
}
echo $i . " ";
}
//Output : 1 3 5 7 9
htmlspecialchars($_SERVER["PHP_SELF"])
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
echo "<p>Hello, " . $name . "!</p>";
}
?>
Instructions:
Helpful Resource:
https://www.w3schools.com/php/php_form_required.asp
University of Tissemsilt Application Web Development 49 / 77
1 About PHP 7 Error/Exception Han-
dling in PHP
2 Data Types
8 File management in
PHP
3 PHP Operators
9 Web 3-tier Architec-
4 PHP Control Structures ture in PHP
<?php
class ClassName {
// Properties (attributes)
// Methods (functions)
}
?>
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
}
$apple = new Fruit("Apple", "red");
<?php
class ClassName {
// Class properties and methods go here
}
<?php
class Person {
public $name;
public $age;
<?php
$person = new Person("John Doe", 30);
$person->displayInfo();
// Output: Name: John Doe, Age: 30
$employee->displayInfo();
/* Output: Name: Jane Smith, Age: 35, Department: Marketing*/
$employee->displayInfo(true);
/* Output: Name: Jane Smith, Age: 35, Salary: 50000,
Department: Marketing */
<?php
$connection = mysqli_connect("host", "user", "password",
"database");
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
However, simply stopping the script is not always the right way to go
error_function(error_level,error_message,
error_file,error_line,error_context)
Parameters :
error_level: Required. The level of the error raised.
error_message: Required. The error message.
error_file: Optional. The filename in which the error was raised.
error_line: Optional. The line number in which the error was raised.
error_context: Optional. Specifies an array containing every variable,
and their values, in use when the error occurred
<?php
function myErrorHandler($severity, $message, $file, $line) {
throw new ErrorException($message, 0, $severity, $file,
$line);
}
set_error_handler("myErrorHandler");
<?php
try {
// Code that may throw an error or exception
} catch (Throwable $t) {
// Handle error or exception
}
<?php
try {
// Risky code (might throw an error)
$result = 10 / 0; // DivisionByZeroError
} catch (Throwable $t) {
echo "Something went wrong: " . $t->getMessage();
}
<?php
$file = fopen("example.txt", "r"); // Opening a File
//Reading a File
$content = fread($file, filesize("example.txt"));
<?php
$content =
file_get_contents('http://www.example.com/somefile.txt');
echo $content;
Or with fopen()
<?php
$file = fopen("http://www.example.com/somefile.txt", "r");
if ($file) {
while (!feof($file)) {
$line = fgets($file);
echo $line;
}
fclose($file);
}
<?php
$ch = curl_init();
$response = curl_exec($ch);
curl_close($ch);
The front-end part of the application. Concerned with how the appli-
cation looks and interacts with users
Receives HTTP requests
Handles user interactions
Implemented using HTML, CSS, JavaScript, PHP
Components: web pages, forms, UI