Alright then, gang! Now that we understand how PHP files are run on the server and how we serve up these pages to the browser using our local development server, let’s take the next step.
Organizing Our Course Files
First things first, let’s keep things tidy. Inside the htdocs
folder, where our files are served from, I’ve created a new folder called torts_tutorials
. This is where we’ll store all our course files for this project or series.
Creating Our First PHP File
Now, let’s dive into coding! I’ve opened up the torts_tutorials
folder in Sublime Text, and I’m going to create our first PHP file. Right-click, select “New File,” and save it as index.php
inside the torts_tutorials
folder.
To write PHP code inside this file, we need to encapsulate it within PHP tags. You can use <?php
to open PHP tags and ?>
to close them. Sublime Text has a handy shortcut where you can just type php
and then hit tab to automatically generate the PHP tags.
A Simple PHP Statement
Let’s start with a simple PHP statement. We’ll use the echo
statement to output a string. Remember, every PHP statement must end with a semicolon ;
. For example, echo "Hello ninjas!";
.
Running Our PHP Code
Now, let’s see our PHP code in action! Open your browser and navigate to localhost/torts_tutorials
. Since we named our file index.php
, we don’t need to specify it in the URL—our local server will automatically look for an index.php
file.
Voila! You should see our message, “Hello ninjas!”, displayed in your browser. But what’s happening behind the scenes?
Understanding PHP Execution
When we request the index.php
page, the server finds the file and executes the PHP code inside it. Our PHP code outputs the string “Hello ninjas!”.
But here’s the kicker: what’s sent back to the browser isn’t actually PHP code—it’s an HTML page. The browser doesn’t understand PHP; it understands HTML. So, the PHP code is executed on the server, and the resulting HTML is sent to the browser for display.
Embedding PHP in HTML
Now, let’s take it up a notch. We can embed PHP directly into our HTML code. For instance, we can dynamically generate HTML content using PHP.
In our index.php
file, let’s create an HTML structure. We’ll add a title and an <h1>
heading, where we’ll dynamically output our “Hello ninjas!” message using PHP’s echo
statement.
Variables and Constants
But wait, there’s more to PHP than just echoing strings! In the next video, we’ll dive into two fundamental concepts: variables and constants. These allow us to store and manipulate data dynamically, taking our PHP skills to the next level.
Understanding Variables
Variables in PHP allow us to store and manipulate data. Think of them as containers for information that can change over time.
To create a variable in PHP, we simply use the $
symbol followed by the variable name, like so: $name
. We can then assign a value to the variable using the assignment operator =
. For example: $name = "John";
.
Variables can store various types of data, including strings (text), integers (whole numbers), floats (decimal numbers), booleans (true/false values), arrays (lists of values), and objects (instances of classes).
Let’s see an example:
$name = “John”;
$age = 30;
$height = 6.2;
$is_student = true;
Using Variables in HTML
One of the powerful aspects of PHP is its ability to seamlessly integrate with HTML. We can use variables to dynamically generate HTML content based on the stored data. For example:
$name = “John”;
echo “<h1>Hello, $name!</h1>”;
This will output: <h1>Hello, John!</h1>
.
Constants in PHP
While variables can change their value throughout the execution of a script, constants remain constant—they don’t change. Constants are useful for storing values that you don’t want to change during the execution of a script, such as configuration settings or fixed values.
To define a constant in PHP, we use the define()
function, passing in the constant name and its value. For example: define("PI", 3.14);
.
Once a constant is defined, it cannot be changed or redefined elsewhere in the script.
Let’s Practice!
Now that we’ve learned about variables and constants, let’s put our knowledge into practice.
Create a PHP file named variables.php
inside the torts_tutorials
folder. In this file, define a few variables and constants, then use them to generate some HTML content.
For example:
<?php
$name = “Alice”;
$age = 25;
define(“PI”, 3.14);
?>
<!DOCTYPE html>
<html lang=“en”>
<head>
<meta charset=“UTF-8”>
<meta name=“viewport” content=“width=device-width, initial-scale=1.0”>
<title>PHP Variables and Constants</title>
</head>
<body>
<h1>Welcome, <?php echo $name; ?>!</h1>
<p>You are <?php echo $age; ?> years old.</p>
<p>The value of PI is <?php echo PI; ?>.</p>
</body>
</html>
Save the file and navigate to localhost/torts_tutorials/variables.php
in your browser to see the result.
Conclusion
Variables and constants are fundamental building blocks in PHP programming. They allow us to store and manipulate data dynamically, making our web applications more powerful and flexible. In the next lesson, we’ll delve into more advanced topics, so stay tuned!