👨🏼‍💻 Part 2: PHP variables

A variable in PHP is like a container that stores information. Think of it like a labeled box where you keep things. The box can hold different types of stuff, like numbers, text, or even more complex data.

Basic Rules of PHP Variables:

✅ A variable in PHP always starts with a $ sign.
✅ It must have a name that starts with a letter or an underscore (_).
✅ The name cannot have spaces or special characters (except _ ).
✅ It can hold different types of data, like numbers, text (strings), or even true/false values.

1. Simple example

<?php
$greeting = "Hello, world!"; // This is a text (string) variable
echo $greeting; // Outputs: Hello, world!
?>

2. A variable can be overwritten

<?php
$greeting = "Hello, world!";$greeting = "Ayeeee, I'm an overwritten variable!";
echo $greeting;  // Outputs the latest $greeting.?>

3. A variable can be extended with “.=” sign.

<?php 

$text = "Hello! ";
$text .= "What is your name? ";
// You can run whatever other PHP commands in between
// The $text variable will remain the same.
$text .= "My name is ".$name;

echo $text; 

?>

4. Combine variables into string

<?php

$greeting = "Hello, world!"; // This is a text (string) variable
$age = 25; // This is a number (integer) variable

echo $greeting; 
echo "<br>"; // HTML line break element (<br> stands for "break")
echo "I am " . $age . " years old."; // Outputs: I am 25 years old

?>
Breakdown of the last example:
  1. $greeting = "Hello, world!"; → Stores text inside a variable.
  2. $age = 25; → Stores a number in a variable.
  3. echo → Displays the values of the variables.
  4. "I am " . $age . " years old." → Joins text and variable values together.

Using variables is essential when writing PHP.

  • They make your code reusable.
  • You can change the values without editing the whole code.
  • They help store and manage data easily.

Independent exercise #1

Create a PHP script that stores personal details in variables and displays a short introduction.

  1. Create a file variables.php
  2. Start the PHP file with <?php tag, and end it with ?> tag
  3. Define the variables
  4. Use the echo function to combine the values of your variables with static text
// Example variables to include
$name = "Alice";
$age = 20;
$color = "blue";
$hobby = "painting";

When visiting the /variables.php file, the result should look like this.

Independent exercise #2

Have you seen some of the websites having an outdated year in the copyright text in their footer?
That’s because they have written the year out as a static text, instead of a dynamic PHP variable.
In this exercise, let’s try our hands on two PHP functions – a function that would return today’s date, and a function that would generate a random number.

Date function
Learn more here

// date() function outputs current time. You can add your own parameters, to define, how PHP should return the time format.
// Let's say it's 28 February, 2025 today, then:
date('d.m.Y'); // returns 28.02.2025
date('Y-m-d'); // returns 2025-02-28
date('d'); // returns 28
$currentYear = date('Y');
echo $currentYear;

// Here we stored the date inside $currentYear variable
// Then echoed it out

Random number generator function
Learn more here

// rand() function outputs a random number.
rand(0,100); // Outputs a random number between 0 and 100.
$random_number = rand(10, 19);
echo "Today I earned " . $random_number . " dollars";

// Outputs "Today I earned 14 dollars"

Write a program, that would output something like this

It should show current year, and random number in a sentence.

Scroll to Top