Mastering Laravel for Web Development
Introduction to PHP
What is PHP?
PHP stands for Hypertext Preprocessor. It's a scripting language that runs on a server, not in your web browser. Its main job is to create dynamic web pages. Think of it as the engine that works behind the scenes to manage data, interact with databases, and build the HTML that your browser displays.
PHP code is written in files that end with .php. Inside these files, you can mix PHP code with regular HTML. The PHP code is enclosed in special tags, <?php and ?>, which tell the server to execute whatever is between them.
Everything outside of the
<?php ... ?>tags is sent directly to the browser as plain HTML.
Let's look at the classic first program: "Hello, World!"
<!DOCTYPE html>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<h1><?php echo "Hello, World!"; ?></h1>
</body>
</html>
Here, the echo command simply tells PHP to output the text that follows it. When the server processes this file, it replaces the PHP block with the text "Hello, World!" before sending the final HTML to your browser.
Variables and Data
To do anything useful, we need to store information. In PHP, we use variables for this. A variable is like a labeled container for a piece of data. All variable names in PHP start with a dollar sign ($).
```php
<?php
$greeting = "Hello";
$user_count = 100;
?>
PHP supports several types of data.
| Data Type | Description | Example |
|---|---|---|
| String | A sequence of characters | $name = "Alice"; |
| Integer | A whole number | $age = 30; |
| Float | A number with a decimal point | $price = 19.99; |
| Boolean | Represents true or false | $is_logged_in = true; |
| Array | A special variable that can hold more than one value | $colors = ["red", "green", "blue"]; |
One of the nice things about PHP is that you don't have to tell it what type of data a variable will hold. It figures it out automatically based on the value you assign.
Controlling the Flow
Programming isn't just about storing data; it's about making decisions and repeating actions. This is handled by control structures. The most common are if statements and loops.
An if statement lets you run code only if a certain condition is true.
```php
<?php
$hour = date('H'); // Get the current hour in 24-hour format
if ($hour < 12) {
echo "Good morning!";
} elseif ($hour < 18) {
echo "Good afternoon!";
} else {
echo "Good evening!";
}
?>
Loops allow you to execute a block of code over and over. A for loop is great when you know exactly how many times you want to repeat an action.
```php
<?php
// This will print the numbers 1 through 5.
for ($i = 1; $i <= 5; $i++) {
echo $i . "\n";
}
?>
Arrays deserve a special mention. They are powerful variables that can store a list of items. You can access individual items using their index, which starts at 0.
```php
<?php
$fruits = ["Apple", "Banana", "Cherry"];
echo $fruits[0]; // Outputs "Apple"
echo $fruits[2]; // Outputs "Cherry"
?>
PHP also supports associative arrays, where you use named keys instead of numbers to access values.
```php
<?php
$user = [
"name" => "John Doe",
"email" => "john.doe@example.com",
"age" => 34
];
echo $user["email"]; // Outputs "john.doe@example.com"
?>
Building with Functions
As your code grows, you'll find yourself writing the same logic in multiple places. Functions help you avoid this repetition. A function is a reusable block of code that performs a specific task. You can give it some data (called parameters), and it can return a result.
Let's create a simple function to greet a user.
```php
<?php
// Define the function
function greetUser($name) {
return "Hello, " . $name . "!";
}
// Call the function and use its return value
echo greetUser("Alice"); // Outputs "Hello, Alice!"
echo greetUser("Bob"); // Outputs "Hello, Bob!"
?>
The
.operator in PHP is used to concatenate (join together) strings.
A Glimpse into Objects
For larger applications, it's helpful to group related data and functions together. This is the core idea behind Object-Oriented Programming (OOP). The two main concepts are classes and objects.
class
noun
A blueprint for creating objects. It defines a set of properties (variables) and methods (functions) that the objects will have.
object
noun
An instance of a class. It's a concrete entity created from the class blueprint, with its own specific values for the properties.
Let's create a simple User class. It will have properties for the user's name and email, and a method to introduce them.
```php
<?php
class User {
// Properties
public $name;
public $email;
// Method
public function introduce() {
return "My name is " . $this->name . ".";
}
}
// Create two objects (instances) from the User class
$user1 = new User();
$user1->name = "Alice";
$user1->email = "alice@example.com";
$user2 = new User();
$user2->name = "Bob";
$user2->email = "bob@example.com";
// Call the method on an object
echo $user1->introduce(); // Outputs "My name is Alice."
?>
Inside a class method, the special variable $this refers to the current object. This allows the introduce() method to access the $name property of the specific user object it was called on.
This is just a quick look, but it gives you an idea of how OOP helps organize code into logical, reusable units. Now, let's review the key terms we've covered.
Ready to test your knowledge?
What does PHP stand for?
Which symbol must every variable name in PHP begin with?
With these fundamentals of PHP syntax, data types, control structures, and functions, you have a solid base to build upon.
