Introduction:
Explore the fundamentals of PHP programming with this comprehensive lesson. From essential string and array functions to file handling and form processing, you’ll gain hands-on experience to build dynamic web applications. Dive into the world of PHP and enhance your web development skills.
PHP has a variety of internal functions, also known as intrinsic functions, that provide a wide range of capabilities for working with variables, strings, arrays, files, databases, and more. Here are some categories of PHP internal functions along with a few examples:
strlen($string): Returns the length of a string.
str_replace($search, $replace, $subject): Replaces all occurrences of a search string with a replacement string.
strpos($haystack, $needle): Finds the position of the first occurrence of a substring in a string.
count($array): Counts the number of elements in an array.
array_push($array, $value): Pushes an element onto the end of an array.
array_merge($array1, $array2): Merges one or more arrays.
abs($number): Returns the absolute value of a number.
round($number, $precision): Rounds a floating-point number to the nearest integer.
rand($min, $max): Generates a random integer.
file_get_contents($filename): Reads entire file into a string.
file_exists($filename): Checks whether a file or directory exists.
fwrite($handle, $string): Writes to an open file.
date($format): Formats a local time/date.
time(): Returns the current Unix timestamp.
strtotime($time, $now): Parses a time string into a Unix timestamp.
mysqli_connect($host, $, $password, $database): Opens a new connection to the MySQL server.
mysqli_query($connection, $query): Performs a query on the database.
session_start(): Starts a new or resumes an existing session.
$_SESSION: An associative array containing session variables.
json_encode($value): Returns the JSON representation of a value.
json_decode($json, $assoc): Decodes a JSON string into a PHP variable.
These are just a few examples, and PHP has many more functions covering various aspects of web development and general-purpose programming.
complete examples in html with explanation
Let’s go through a few examples of PHP usage within HTML, explaining each one:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>PHP String Example</title> </head> <body> <?php $name = "Omar"; $greeting = "Hello, " . $name . "!"; echo $greeting; ?> </body> </html>
Explanation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Array and Loop Example</title>
</head>
<body>
<?php
$colors = array("Red", "Green", "Blue");
echo "<ul>";
foreach ($colors as $color) {
echo "<li>$color</li>";
}
echo "</ul>";
?>
</body>
</html>
Explanation:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Form Handling Example</title>
</head>
<body>
<?php
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve form data
$name = $_POST["name"];
$password = $_POST["password"];
// Process and display data
echo "Welcome, $name! Your password is: $password";
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="name">name:</label>
<input type="text" name="name" required>
<label for="password">Password:</label>
<input type="password" name="password" required>
<button type="submit">Submit</button>
</form>
</body>
</html>
Explanation:

These examples showcase some common use cases of PHP within HTML, including string manipulation, array handling, loops, and form processing.
Let’s go through a couple more examples covering different aspects such as working with files and incorporating conditional statements.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP File Handling Example</title>
</head>
<body>
<?php
// Read content from a file
$filename = "example.txt";
if (file_exists($filename)) {
$content = file_get_contents($filename);
echo "<p>File Content: $content</p>";
} else {
echo "<p>File not found!</p>";
}
// Write to a file
$newContent = "This is a new line of content.";
file_put_contents($filename, $newContent, FILE_APPEND | LOCK_EX);
echo "<p>Content added to file!</p>";
?>
</body>
</html>
Explanation:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PHP Conditional Statements Example</title>
</head>
<body>
<?php
$temperature = 25;
// Conditional statement
if ($temperature < 20) {
echo "<p>It's cold outside!</p>";
} elseif ($temperature >= 20 && $temperature < 30) {
echo "<p>It's a pleasant day.</p>";
} else {
echo "<p>It's warm outside!</p>";
}
?>
</body>
</html>
Explanation:
These examples provide additional insights into working with files, reading and writing content, and using conditional statements in PHP within an HTML context.
Let’s create a simple PHP application that combines several concepts, including form handling, file operations, and conditional statements.
In this example, we’ll create a basic “To-Do List” application where s can add and view tasks.
To-Do List Application
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple To-Do List</title>
</head>
<body>
<?php
// File to store tasks
$filename = "tasks.txt";
// Check if the form is submitted
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Retrieve task from the form
$task = $_POST["task"];
// Append task to the file
file_put_contents($filename, $task . PHP_EOL, FILE_APPEND | LOCK_EX);
}
// Display tasks from the file
if (file_exists($filename)) {
$tasks = file($filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!empty($tasks)) {
echo "<h2>Tasks:</h2>";
echo "<ul>";
foreach ($tasks as $task) {
echo "<li>$task</li>";
}
echo "</ul>";
} else {
echo "<p>No tasks yet. Add one using the form below.</p>";
}
} else {
echo "<p>No tasks yet. Add one using the form below.</p>";
}
?>
<h2>Add a Task:</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<label for="task">Task:</label>
<input type="text" name="task" required>
<button type="submit">Add Task</button>
</form>
</body>
</html>
Explanation:
Form Handling:
The application checks if the form is submitted using $_SERVER[“REQUEST_METHOD”].
If submitted, it retrieves the task from the form ($_POST[“task”]) and appends it to the “tasks.txt” file using file_put_contents.
Displaying Tasks:
Checks if the file “tasks.txt” exists.
If the file exists, reads its content using file function and displays the tasks in an unordered list (<ul>).
Conditional Statements:
Uses conditional statements to check if there are tasks to display and provides appropriate messages.
HTML Form:
Provides a simple HTML form where s can input tasks.
This basic To-Do List application allows s to add tasks via a form, and it displays the list of tasks. The tasks are stored in a text file, making the data persistent across page reloads.

Here’s a quiz about the PHP lesson:
PHP Basics Quiz:
a) Personal Home Page
b) Preprocessed Hypertext Page
c) PHP: Hypertext Processor
d) Preformatted Hyperlink Protocol
a) string_length()
b) strlen()
c) str_length()
d) lengthOfString()
a) Reads a file into an array
b) Reads entire file into a string
c) Writes content to a file
d) Checks if a file exists
a) $colors = array(“Red”, “Green”, “Blue”);
b) $colors = [“Red”, “Green”, “Blue”];
c) $colors = “Red”, “Green”, “Blue”;
d) $colors = (“Red”, “Green”, “Blue”);
a) file_exists()
b) check_file()
c) is_file()
d) file_check()
a) Display output
b) Perform arithmetic operations
c) Include a file
d) Retrieve form data
a) while
b) for
c) foreach
d) do-while
a) Server settings
b) Session variables
c) System variables
d) Super globals
a) round_to_int()
b) ceil()
c) floor()
d) round()
a) json_encode()
b) encode_json()
c) json_stringify()
d) encode_to_json()
a) Reads content from a file
b) Deletes a file
c) Appends content to a file
d) Writes content to a file
a) Current PHP version
b) Server hostname
c) Current script filename
d) PHP configuration settings
a) random()
b) generate_random()
c) rand()
d) random_number()
a) session_start()
b) start_session()
c) new_session()
d) init_session()
a) Converts special characters to HTML entities
b) Encodes a variable into JSON format
c) Checks if a file exists
d) Rounds a floating-point number
1-c) PHP: Hypertext Processor
2-b) strlen()
3-b) Reads entire file into a string
4-b) $colors = [“Red”, “Green”, “Blue”];
5-a) file_exists()
6-a) Display output
7-c) foreach
8-b) Session variables
9-d) round()
10-a) json_encode()
11-c) Appends content to a file
12-c) Current script filename
13-c) rand()
14-a) session_start()
15-a) Converts special characters to HTML entities