PHP Code Sample: A Comprehensive Guide
PHP is a widely used scripting language for web development. It is known for its simplicity, flexibility, and extensive support for various databases. In this article, we will explore some useful PHP code samples that can enhance your web development skills.
1. Hello World!
Let's start with the classic "Hello World!" example. This simple code snippet demonstrates the basic syntax of PHP:
<?php
echo "Hello World!";
?>
When you run this code, it will display "Hello World!" on the screen. This example showcases the echo statement, which is used to output text or variables.
2. Variables and Data Types
PHP supports various data types, including strings, integers, floats, booleans, arrays, and objects. Here's an example that demonstrates variable declaration and data type usage:
<?php
$name = "John Doe";
$age = 25;
$height = 1.75;
$isStudent = true;
$skills = array("PHP", "HTML", "CSS");
?>
In this code, we declare variables for a person's name, age, height, student status, and skills. The variables are assigned different data types based on their values.
3. Conditional Statements
Conditional statements allow you to execute different blocks of code based on certain conditions. Here's an example that demonstrates the if-else statement:
<?php
$grade = 85;
if ($grade >= 90) {
echo "Excellent!";
} elseif ($grade >= 80) {
echo "Good!";
} else {
echo "Needs Improvement!";
}
?>
In this code, the if-else statement checks the value of the $grade variable and displays a corresponding message based on the condition.
4. Loops
Loops are used to execute a block of code repeatedly. PHP provides several loop structures, including for, while, and foreach. Here's an example that demonstrates the foreach loop:
<?php
$fruits = array("Apple", "Banana", "Orange");
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
This code iterates over the $fruits array and displays each fruit on a new line using the echo statement.
5. Database Connectivity
PHP has excellent support for database connectivity. Here's an example that demonstrates connecting to a MySQL database and executing a query:
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "mydatabase";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Execute query
$sql = "SELECT * FROM users";
$result = $conn->query($sql);
// Process result
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "Name: " . $row["name"] . "<br>";
}
} else {
echo "No results found.";
}
// Close connection
$conn->close();
?>
This code establishes a connection to a MySQL database, executes a SELECT query, and displays the names of the users retrieved from the database.
Conclusion
These PHP code samples provide a glimpse into the versatility and power of PHP for web development. By understanding and practicing these examples, you can enhance your PHP skills and create dynamic and interactive web applications.
For more information on PHP and its applications in VPS hosting, visit Server.HK.