Learning PHP

Table of contents

Introduction

Why use PHP

Setup

PHP Basics

PHP Example Use Cases

Additional Resources

Introduction

PHP stands for Hypertext Preprocessor, and it is a server-side scripting language designed for web development written in C. This makes it different from client-side languages such as JavaScript, which is executed on the client’s browser rather than the web server. PHP is embedded within HTML code and is widely used to create dynamic and interactive web pages. It can be used to perform various tasks such as collecting form data, generating dynamic page content, managing databases, handling cookies and sessions, and more.

Before continuing to learn PHP it is recommended to have a basic understanding of the commmon web development languages:

Why use PHP

You may have heard PHP reffered to as a “dying language”, but it is still widely used in web development. In fact, it is estimated that over 75% of websites use PHP in some way. PHP is a great choice for web development because it is easy to learn and use, as well as being free and open-source.

Additional advantages of PHP include:

You can learn more about the advantages and disadvantages of PHP here.

Setup

To set up PHP on your machine, you will need:

The official PHP website provides a guide on how to install PHP on your machine. Additionaly, you can find a video tutorial on how to install PHP on Windows here.

PHP Basics

You can write PHP scripts at any point in an HTML document. The syntax for PHP scripts is as follows:

<?php
    // PHP code goes here
?>

As we can see the script starts with <?php and ends with ?>. Any code in between these tags will be executed by the PHP engine.

To demonstrate how PHP works, let’s create a simple “Hello World” static web page. First, create a new file called index.php and add the following code:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

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

</body>
</html>

In the above example, we have a simple HTML document with a heading and a PHP script that prints “Hello World!”. If we open this file in a browser, we will see the following output:

My first PHP page
Hello World!

As we can see, the PHP script is embedded directly within the HTML code. By using echo, we can output text to the browser.

However the above example is the exact same as this static HTML page:

<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

Hello World!

</body>
</html>

Let’s take a look at a few more complex examples of PHP in action.

PHP Example Use Cases

Variables

Variables are used to store information. PHP has no command for declaring a variable. A variable is created the moment you first assign a value to it. Variables in PHP start with the $ sign, followed by the name of the variable. Variable names are case-sensitive.

<?php
    $txt = "Hello World!";
    $x = 5;
    $y = 10.5;
?>

In the above example, we have created three variables: $txt, $x, and $y. $txt stores the string “Hello World!”, $x stores the integer 5, and $y stores the float 10.5. We can output the values of these variables in our HTML document using echo:

<!DOCTYPE html>
<html>
<body>

<?php
    // Display variables in a HTML list
    $txt = "Hello World!";
    $x = 5;
    $y = 10.5;
    echo "<ul>";
    echo "<li>$txt</li>";
    echo "<li>$x</li>";
    echo "<li>$y</li>";
    echo "</ul>";
?>

</body>
</html>

You can see that in the above echo calls we don’t just use the variables, but we also include HTML tags. By employing more powerful logic, we can make more complex reactive web pages, as PHP can be used to dynamically generate HTML content.

In addition to variables PHP has much of the functionality you would expect from a programming language, such as loops, conditionals, functions, and more. You can learn more about the specific PHP syntax for those operations here.

JavaScript

We can also run JavaScript code directly in PHP. This allows us to use PHP together with JavaScript to create dynamic web pages.

<!DOCTYPE html>
<html>
<body>

<?php
    // Generate a random number between 1 and 10
    $randomNumber = rand(1, 10);
    echo "<p>Random number: $randomNumber</p>";
?>

<script>
    // Use the random number in a JavaScript function
    function multiplyByRandomNumber(num) {
        return num * <?php echo $randomNumber; ?>;
    }
    document.write('<p>5 * random number = ' + multiplyByRandomNumber(5) + '</p>');
</script>

</body>
</html>

In the example above we use the PHP rand() function to generate a random number between 1 and 10 and store it in the $randomNumber variable. We then use the PHP echo function to output the value of $randomNumber in the JavaScript function. This allows us to use the PHP variable in the JavaScript function.

Forms

PHP can also be used to handle form data. In the following example, we have a simple HTML form that asks the user to enter their name. When the user submits the form, the PHP script will display the name they entered.

<!DOCTYPE html>
<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
    Name: <input type="text" name="name">
    <input type="submit">
</form>

<?php
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        // collect value of input field
        $name = $_POST['name'];
        if (empty($name)) {
            echo "Name is empty";
        } else {
            echo "Hello $name!";
        }
    }
?>

</body>
</html>

Databases

PHP can also be used to connect to databases. In the following example, we connect to a MySQL database and display the contents of a table.

<!DOCTYPE html>
<html>
<body>

<?php
// Replace these values with your actual database credentials
$servername = "your_servername";
$username = "your_username";
$password = "your_password";
$dbname = "your_dbname";

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

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

// SQL query to retrieve data from the table
$sql = "SELECT * FROM example_table";
$result = $conn->query($sql);

// Display table if there are results
if ($result->num_rows > 0) {
    echo "<table>";
    echo "<tr><th>ID</th><th>Name</th><th>Description</th></tr>";

    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr><td>" . $row["id"] . "</td><td>" . $row["name"] . "</td><td>" . $row["description"] . "</td></tr>";
    }

    echo "</table>";
} else {
    echo "0 results";
}

// Close connection
$conn->close();
?>

</body>
</html>

The above examples are just a few of the many ways PHP can be used. You can learn more about PHP and its extensive uses through the following resources.

Additional Resources