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 - Basic Operators and String Concatenation

Basic Operators and String Concatenation - Images: Japanese

Growing up in Tokyo my whole life, I was a complete city kid — and apparently that shaped how I saw the world in some pretty odd ways.

As a young child, I was absolutely convinced that rivers were "green" and the ocean was "black."

One day, I painted a picture with a green river and a black ocean and turned it in at school...

...and my teacher gently suggested I go see a doctor. I ended up getting some kind of psychological evaluation. The whole experience left quite an impression on me.

Anyway! Hi everyone, hope you're doing well.

Tokyo's rivers have actually gotten a lot cleaner lately thanks to better sewage treatment — they're almost clear now. But the Kanda River is still very much... green. Or maybe black. Growing up around dirty rivers gives them a kind of charm, honestly. Pristine rivers feel almost too foreign — like a place you don't belong.

Dirty rivers, murky seas, thick air, and a GDP that breaks every chart — Tokyo is truly something else.

Anyway, back to PHP. Last time we covered include, which showed off some of PHP's real power. This time, let's look at the basics of 'operators' and, while we're at it, 'string concatenation'.

So what is an "operator"? In a word, it's a symbol or character that performs a special operation. Think back to math class — you've got the '+' sign in something like "1 + 2". That '+' is an operator.

PHP has several kinds of operators. Today we'll cover 'arithmetic operators', 'string operators', and 'assignment operators'. You don't need to memorize those names — they mostly show up in textbooks. Just make sure you remember the word "operator".

Let's start with arithmetic operators. These are used for addition, subtraction, multiplication, and division — basic math, in other words.

In PHP: addition uses '+', subtraction uses '-', multiplication uses '*', division uses '/', and the remainder (modulo) uses '%'. These are consistent across almost every programming language, so they're worth memorizing. You'll find the same symbols on most calculator apps too.

The values or variables that an operator acts on are called 'operands'.

For example, in "1 + 2", the numbers '1' and '2' are both operands.

You'll see this term in more advanced textbooks, so it's good to keep it in mind.

Using arithmetic operators is straightforward — just write out the operation you want to perform.

<?php
echo 1 + 2; // Outputs the result of '1 + 2'.
echo 3 - 2; // Outputs the result of '3 - 2'.
echo 3 * 2; // Outputs the result of '3 × 2'.
echo 6 / 2; // Outputs the result of '6 ÷ 2'.
echo 6 % 5; // Outputs the remainder of '6 ÷ 5'.

$test = 1 + 2; // Calculates '1 + 2' and assigns the result, 3, to the variable 'test'.

PHP sometimes performs automatic type conversion.

For example, adding the string '1' to the number 2 — that is, '1' + 2 — won't cause an error. PHP automatically converts the string '1' to the number 1, and returns 3 as the result.

This might seem convenient, but type conversion is happening behind the scenes without you explicitly asking for it, which can lead to unexpected bugs. Be careful with this.

<?php
echo '1' + 2; // The string '1' is auto-converted to the number 1, so 3 is output.
echo true + 0; // The boolean 'true' converts to 1, so '1 + 0' outputs 1.
echo false + 0; // The boolean 'false' converts to 0, so '0 + 0' outputs 0.

Now for a handy programming trick: 'increment'. This increases a variable's value by 1. Here's how it looks.

<?php
$x = 0;
++$x; // This is the same as writing '$x = $x + 1'.

echo $x; // Outputs '1'.

Writing '++' before a variable adds 1 to it. Adding 1 is one of the most common operations in programming, so the increment operator gets used all the time.

The opposite is '--', which subtracts 1 from a variable. This is called 'decrement'.

<?php
$x = 1;
--$x; // This is the same as writing '$x = $x - 1'.

echo $x; // Outputs '0'.

Increment and decrement exist in virtually every programming language, so it's worth learning them as a pair.

In PHP, increment and decrement can be written in two ways: prefix (before the variable) and postfix (after the variable). Here's what that looks like with increment — the same applies to decrement.

<?php
$a = 0;
$b = 0;

++$a;
$b++;

var_dump($a); // This is '1'.
var_dump($b); // This is '1'.

Both variables 'a' and 'b' get incremented by 1, so both output '1'. When used on their own like this, there's no difference between prefix and postfix.

However, when combined with an assignment, prefix and postfix behave differently. Take a look at this example.

<?php
$a = 0;
$b = 0;

$c = ++$a;
$d = $b++;

var_dump($a); // This is '1'.
var_dump($b); // This is '1'.
var_dump($c); // This is '1'.
var_dump($d); // This is '0'.

We start by assigning 0 to variables 'a' and 'b', then apply prefix and postfix increment. We then assign those results to variables 'c' and 'd'.

Variables 'a' and 'b' both end up as '1', but what about 'c' and 'd'? As you can see in the comments, 'c' is '1' and 'd' is '0'.

The reason is that postfix increment first returns the current value, and then adds 1. So in this case, the current value of 'b' (which is 0) gets assigned to 'd' first, and then 'b' is incremented to 1.

In other words, this code is equivalent to writing it out like this:

<?php
$a = 0;
$b = 0;

// This is the '$c = ++$a' pattern
$a = $a + 1;
$c = $a;

// This is the '$d = $b++' pattern
$d = $b;
$b = $b + 1;

var_dump($a); // This is '1'.
var_dump($b); // This is '1'.
var_dump($c); // This is '1'.
var_dump($d); // This is '0'.

That was a lot to take in, and it can get confusing. The safe bet is to always use the prefix pattern for both increment and decrement — it does exactly what most people expect.

Prefix also has a slight performance edge, since postfix has to return the original value before incrementing, which adds a tiny bit of overhead behind the scenes.

So when in doubt, just use prefix and you'll be fine.

That said, postfix isn't wrong by any means — if you have a situation where it makes sense, go ahead and use it. Use whichever fits the context.

Strings are extremely important in web development.

PHP — that ruggedly web-focused, thoroughly no-nonsense language — takes string handling seriously, and it shows: you can actually increment and decrement strings.

<?php
$txt = 'a';
$txt++; // Increments the string 'a' to 'b'.

echo $txt; // Outputs 'b'.

The letter 'a' became the next letter 'b'. Incrementing 'z' wraps around and becomes 'aa', and incrementing 'az' gives you 'ba'.

You might not use this very often, but it's a neat feature to have in your back pocket.

Next up: string operators. In PHP, you can concatenate strings using a '.' (dot). Here's an example.

<?php
echo 'Hello' . ' world'; // Outputs the string 'Hello world'.

If you concatenate a non-string value with a string, it gets automatically converted to a string first.

<?php
$num = 2015; // Assigns the number 2015 to the variable 'num'.

echo 'This year is ' . $num . '.'; // Outputs the string 'This year is 2015.'.

One thing to watch out for: if you want to concatenate an integer directly (not stored in a variable), you need a space on either side of the dot. That's because '.' is also used for decimal points like '3.14'. If you put a dot right next to an integer without spaces, PHP may think you're writing a decimal number and throw an error depending on the context.

<?php
echo 'At one point, pi was approximated as not 3.14 but' . 3 .'exactly.'; // This works.

echo 'At one point, pi was approximated as not 3.14 but'.3.'exactly.'; // This causes an error — no spaces around the dot.

To be safe, always put spaces on both sides of the '.' when concatenating strings.

Also worth noting: many other languages use '+' for string concatenation, but PHP uses '.'. Don't mix them up.

Next is the 'assignment operator'. This is the '=' you've already seen many times — it's used to assign a value to a variable.

<?php
$txt = 'Hello world'; // Assigns the string 'Hello world' to the variable 'txt'.

There's also a related group called 'compound assignment operators' that we haven't covered yet. Using '+=', you can add a value to a variable and assign the result back to itself. That sounds a bit abstract, so let's look at an example.

<?php
$num = 1; // Assigns the number 1 to the variable 'num'.
$num += 2; // This is the same as writing '$num = $num + 2;'.

echo $num; // Outputs the number 3.

We saw a similar pattern a few articles back — using a variable in its own calculation and assigning the result back. This is just a shorthand for that. Think of it as "add more to yourself". It takes a little getting used to, but this syntax appears in almost every programming language, so you really do need to learn it.

Beyond '+=', PHP also offers '-=', '*=', '/=', '%=', and '.='.

<?php
$num1 = 10; // Assigns the number 10 to the variable 'num1'.
$num1 += 2; // Same as '$num1 = $num1 + 2;'.
echo $num1; // Outputs 12.

$num2 = 10; // Assigns the number 10 to the variable 'num2'.
$num2 -= 2; // Same as '$num2 = $num2 - 2;'.
echo $num2; // Outputs 8.

$num3 = 10; // Assigns the number 10 to the variable 'num3'.
$num3 *= 2; // Same as '$num3 = $num3 * 2;'.
echo $num3; // Outputs 20.

$num4 = 10; // Assigns the number 10 to the variable 'num4'.
$num4 /= 2; // Same as '$num4 = $num4 / 2;'.
echo $num4; // Outputs 5.

$num5 = 10; // Assigns the number 10 to the variable 'num5'.
$num5 %= 3; // Same as '$num5 = $num5 % 3;'.
echo $num5; // Outputs 1.

$txt = 'Hello'; // Assigns the string 'Hello' to the variable 'txt'.
$txt .= ' world'; // Same as '$txt = $txt . ' world';'.
echo $txt; // Outputs 'Hello world'.

There are other compound assignment operators too, but given PHP's nature as a web language, these six are the ones you'll actually use most. Focus on learning these well.

And that wraps up the basics of operators and string concatenation. In the next article, we'll tackle 'arrays' — another concept that trips up a lot of beginners right after variables.

That's all for now. See you next time!

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 .