Welcome to The Coding College! In this tutorial, we will explore some practical PHP examples that cover basic and advanced concepts. These examples will help you understand PHP better and learn how to use it efficiently for web development.
1. Hello World Example
A simple PHP script to output “Hello, World!”.
<?php
echo "Hello, World!";
?>
Output:
Hello, World!
2. PHP Variables Example
Declaring and printing variables in PHP.
<?php
$name = "John Doe";
$age = 25;
echo "Name: " . $name . "<br>";
echo "Age: " . $age;
?>
Output:
Name: John Doe
Age: 25
3. PHP If-Else Statement Example
Using conditional statements to check values.
<?php
$score = 85;
if ($score >= 90) {
echo "Grade: A";
} elseif ($score >= 75) {
echo "Grade: B";
} else {
echo "Grade: C";
}
?>
Output:
Grade: B
4. PHP For Loop Example
Using a for
loop to display numbers from 1 to 5.
<?php
for ($i = 1; $i <= 5; $i++) {
echo "Number: $i <br>";
}
?>
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
5. PHP Array Example
Creating and accessing an indexed array.
<?php
$colors = array("Red", "Green", "Blue");
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
Output:
Red
Green
Blue
6. PHP Associative Array Example
Using an associative array to store key-value pairs.
<?php
$student = array("Name" => "Alice", "Age" => 22, "Grade" => "A");
echo "Name: " . $student["Name"] . "<br>";
echo "Age: " . $student["Age"] . "<br>";
echo "Grade: " . $student["Grade"];
?>
Output:
Name: Alice
Age: 22
Grade: A
7. PHP Function Example
Defining and calling a function.
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice");
?>
Output:
Hello, Alice!
8. PHP Form Handling Example
A simple example to handle user input using HTML and PHP.
HTML Code
<!DOCTYPE html>
<html>
<body>
<form action="form_handler.php" method="post">
Name: <input type="text" name="name"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
PHP Code (form_handler.php)
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_POST["name"];
echo "Hello, " . htmlspecialchars($name) . "!";
}
?>
Output (when the user submits “John”):
Hello, John!
9. PHP File Handling Example
Reading the contents of a file.
example.txt
(Content)
Hello, this is a sample file.
PHP Code
<?php
$filename = "example.txt";
$file_content = file_get_contents($filename);
echo "File Content: <br>";
echo nl2br($file_content);
?>
Output:
File Content:
Hello, this is a sample file.
10. PHP Database Connection Example
Connecting to a MySQL database.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "my_database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
?>
Output:
Connected successfully
11. PHP OOP – Classes and Objects Example
Creating a class and an object in PHP.
<?php
class Car {
public $brand;
public $color;
function __construct($brand, $color) {
$this->brand = $brand;
$this->color = $color;
}
function displayDetails() {
echo "Car: " . $this->brand . ", Color: " . $this->color;
}
}
$myCar = new Car("Toyota", "Red");
$myCar->displayDetails();
?>
Output:
Car: Toyota, Color: Red
12. PHP Date and Time Example
Displaying the current date and time.
<?php
echo "Current Date and Time: " . date("Y-m-d H:i:s");
?>
Output:
Current Date and Time: 2024-06-17 12:30:45
Conclusion
These examples cover some of the most commonly used features of PHP, from basic scripting to database connections and OOP concepts. By practicing these examples, you can build a solid foundation in PHP programming.
Next Steps:
- Try modifying these examples to suit your project needs.
- Explore more advanced topics like sessions, cookies, and APIs.