Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

PHP Comprehensive Tutorial

Introduction to PHP

PHP (Hypertext Preprocessor) is a popular server-side scripting language designed for web development. It is embedded within HTML and is used to manage dynamic content, databases, session tracking, and even build entire e-commerce sites. PHP is known for its simplicity, speed, and flexibility.

Setting Up PHP

To run PHP, you need to install a web server with PHP support. The most common setup is the XAMPP or LAMP stack.

For Windows:

Download and install XAMPP.

Start the Apache server from the XAMPP control panel.

For Linux:
                sudo apt update
                sudo apt install apache2
                sudo apt install php libapache2-mod-php
                sudo systemctl restart apache2
                

Basic PHP Syntax

PHP scripts are executed on the server. A PHP script starts with <?php and ends with ?>.

<?php
echo "Hello, World!";
?>
                

Save the above code in a file with a .php extension (e.g., index.php) and place it in your web server's root directory. Access the file through your web browser to see the output.

PHP Variables

Variables in PHP start with a dollar sign ($). PHP automatically converts the variable to the correct data type, depending on its value.

<?php
$txt = "Hello, World!";
$number = 123;
echo $txt;
echo $number;
?>
                

PHP Data Types

PHP supports several data types including strings, integers, floats, booleans, arrays, objects, NULL, and resources.

<?php
$str = "Hello";
$int = 123;
$float = 12.34;
$bool = true;
$arr = array("apple", "banana", "cherry");
$obj = (object) ['property' => 'value'];
$null = NULL;
?>
                

PHP Conditional Statements

PHP supports several conditional statements: if, else, elseif, and switch.

<?php
$number = 10;

if ($number > 0) {
    echo "Positive number";
} elseif ($number < 0) {
    echo "Negative number";
} else {
    echo "Zero";
}
?>
                

PHP Loops

PHP supports several looping structures: while, do...while, for, and foreach.

<?php
for ($i = 0; $i < 5; $i++) {
    echo "The number is: $i <br>";
}
?>
                

PHP Functions

Functions are blocks of code that can be repeatedly called. PHP has many built-in functions, and you can also create your own functions.

<?php
function greet($name) {
    return "Hello, $name!";
}

echo greet("World");
?>
                

PHP and HTML Forms

PHP can handle form data sent via GET and POST methods. Here's a simple form submission example:

<!-- HTML form -->
<form method="post" action="process.php">
    Name: <input type="text" name="name">
    <input type="submit">
</form>

<!-- PHP code in process.php -->
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = htmlspecialchars($_POST['name']);
    echo "Hello, $name!";
}
?>
                

PHP and MySQL

PHP can interact with databases, most commonly MySQL. You can use the mysqli extension or PDO (PHP Data Objects) to connect and interact with MySQL databases.

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
                

Conclusion

This tutorial provided an overview of PHP, from basic syntax to database interactions. PHP is a powerful and flexible language that can be used for a wide range of web development tasks. Practice and experimentation are key to mastering PHP.