No history yet

PHP Basics

The Building Blocks of PHP

PHP code lives inside special tags that tell the server, "Hey, this part is for me to process!" The server then runs the code and sends the result, usually plain HTML, back to the browser. This all happens on the server, which is why PHP is called a server-side language.

The basic tags look like this: <?php to open, and ?> to close. Anything inside these tags is PHP code.

A common way to output text is with the echo statement. It's simple and gets the job done.

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

Notice the semicolon at the end of the line. In PHP, most statements need to end with a semicolon. It's like the period at the end of a sentence, telling PHP you're done with that instruction.

Storing Information with Variables

Variables are containers for storing information. In PHP, a variable always starts with a dollar sign ($). You don't have to tell PHP what type of data you're storing; it figures it out automatically. You can store text, numbers, or simple true/false values.

string

noun

A sequence of characters, like a word or a sentence. In code, strings are wrapped in quotes.

Let's look at the most common data types.

<?php
// String: A sequence of text characters
💲greeting = "Welcome to our site!";

// Integer: A whole number
💲visitor_count = 150;

// Float: A number with a decimal point
💲price = 19.99;

// Boolean: A true or false value
💲is_logged_in = true;

// You can then use these variables
echo 💲greeting;
echo "Current visitors: ";
echo 💲visitor_count;
?>

You can also combine, or concatenate, strings using a period (.).

<?php
💲first_name = "Alex";
💲last_name = "Smith";

// Combine the two strings with a space in between
💲full_name = 💲first_name . " " . 💲last_name;

echo 💲full_name; // Outputs "Alex Smith"
?>

Making Decisions and Repeating Actions

A program's real power comes from its ability to make decisions and perform repetitive tasks. PHP uses control structures for this. They control the flow of your script.

The if statement is the most basic decision-making tool. It checks if a condition is true and runs a block of code only if it is.

<?php
💲hour = 14; // Represents 2 PM

if (💲hour < 12) {
  echo "Good morning!";
} elseif (💲hour < 18) {
  echo "Good afternoon!";
} else {
  echo "Good evening!";
}
?>

In this example, because $hour is 14 (which is greater than 12 but less than 18), the script will output "Good afternoon!". The elseif lets you check another condition, and else is a fallback for when no conditions are met.

When you need to do something over and over, you use a loop. A for loop is perfect when you know exactly how many times you want to repeat an action.

<?php
// This loop will run 5 times
for (💲i = 1; 💲i <= 5; 💲i++) {
  echo "This is loop number " . 💲i . "<br>";
}
?>

Here’s how it works: \$i = 1 starts a counter. \$i <= 5 is the condition that keeps the loop running. \$i++ increases the counter by one after each cycle.

Creating Reusable Code with Functions

Imagine you have a task you need to perform multiple times, like calculating sales tax. Instead of writing the same code again and again, you can package it into a function.

A function is a named block of code that you can call whenever you need it. This makes your code cleaner, easier to read, and simpler to update.

You define a function using the function keyword. You can also pass data into it using parameters, which act like variables inside the function.

<?php
// Define a function to calculate the total price
function calculate_total(💲price, 💲tax_rate) {
  💲total = 💲price + (💲price * 💲tax_rate);
  return 💲total;
}

// Call the function with different values
💲item1_total = calculate_total(100, 0.07);
💲item2_total = calculate_total(50, 0.07);

echo "Item 1 costs: 💲" . 💲item1_total;
// Outputs "Item 1 costs: 💲107"
?>

In this example, calculate_total takes a price and a tax rate as input. The return keyword sends the final value back out of the function, where it can be stored in a variable like \$item1_total.

Let's check your understanding of these core concepts.

Quiz Questions 1/7

How do you correctly open and close a block of PHP code?

Quiz Questions 2/7

True or False: PHP is called a "server-side" language because its code is executed on the web server before the result is sent to the user's browser.