Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

  1. Home
  2. PHPBeginner - How to Use Variables

How to Use Variables - Images: Japanese

Hey everyone, hope you're doing well!

Last time we covered the overview of 'variables' and the basics of how to write them. But we hadn't gotten to the really important part yet — actually using variables. So that's what we're tackling today.

Variables are incredibly versatile, and once you get the hang of them, you can build some really interesting logic and keep your code easy to maintain. There are way too many usage patterns to cover them all, so let's walk through just a few examples.

Alright, bear with me here — let's do a little thought experiment.

Imagine you wake up one morning feeling so amazingly refreshed that you suddenly want to say "Good morning!" to every single person in the world. About a million times. It's just that kind of morning.

Of course, physically greeting a million people is impossible. So you decide to do it through the internet.

You spend two hours building a PHP script that outputs "Good morning!" one million times, and you publish the website.

<?php
echo 'Good morning!';
echo 'Good morning!';
echo 'Good morning!';
echo 'Good morning!';
echo 'Good morning!';
// ...imagine this goes on for one million lines.

But those two hours flew by, and now the world has moved on from morning to midday.

You panic. At this rate, you're about to greet the entire world with "Good morning!" in the middle of the afternoon. You need to change it to "Hello!" — fast. But it's too late.

Despite your best efforts, you end up saying "Good morning!" to everyone at noon. What a disaster.

(Yeah okay, you could just use find-and-replace... but stay with me here.)

Now think about what would have happened if you'd used a 'variable'.

You see it already, don't you? With a variable, you'd only need to change one line to update every single output to "Hello!" in an instant.

<?php
$text = 'Good morning!'; // Change this one line to update all outputs at once.

echo $text;
echo $text;
echo $text;
echo $text;
echo $text;
// ...imagine this goes on for one million lines.

Now you're covered no matter what time of day it is — morning, noon, or night.

Okay, that was quite the scenario, but it illustrates the first use case I wanted to show you. Variables can be called as many times as you like, so whenever you need to output the same value repeatedly, using a variable makes your code far easier to maintain.

Now here's another example I want to share. A variable can use itself in a calculation and then store the result back into itself. It sounds a little weird at first.

Take a look at this sample. Can you guess what it outputs? This comes up in other articles too — it's a tricky kind of operation that doesn't exist in math but is very much a thing in programming.

<?php
$x = 1;
$x = $x + 1;

echo $x;

The answer is 2. Let me walk through it. If you're already thinking "obviously — I don't need the explanation", feel free to skip ahead.

The first line is straightforward — we're just putting the number '1' into the variable '$x'.

$x = 1;

The tricky part is the next line.

$x = $x + 1;

This is where a lot of people get tripped up. Think back to what we covered in the previous article — in programming, the right side is evaluated first, then the result is assigned to the left side. So let's focus on the right side. At the moment the right side runs, '$x' still holds '1'. So the expression becomes:

$x + 1; // $x is 1 at this point, so this is just 1 + 1.

That evaluates to '2', and then that value gets assigned to '$x':

$x = 2;

So the answer is 2. Programming has a lot in common with math — at its core, it basically is math — but this kind of self-referential assignment is a distinctly programming-style trick you won't find in a textbook.

And that wraps up our little tour of variable usage examples. Did you get a sense of just how powerful the concept of 'variables' can be?

We've been covering some pretty foundational stuff lately, so next time we'll get into something more exciting — a powerful PHP feature you won't find in plain HTML: include(). Looking forward to it!

That's all for now — see you in the next one!

This article was written by Sakurama.

Author's beloved small mammal

桜舞 春人 Sakurama Haruto

A Tokyo-based programmer who has been creating various content since the ISDN era, with a bit of concern about his hair. A true long sleeper who generally feels unwell without at least 10 hours of sleep. His dream is to live a life where he can sleep as much as he wants. Loves games, sports, and music. Please share some hair with him.

If you find any errors or copyright issues, please .