Logo
/
Programming
/
PHP Course
/

Variables and concatenation

PHP: Variables and concatenation

Earlier we already glued strings together directly using concatenation. Now let's do the same thing, but with variables. Good news: the syntax stays the same. The values of the variables are simply substituted.

Gluing two strings directly

<?php

$what = 'Kings' . 'road';
print_r($what); // => Kingsroad

Everything is simple here: two strings are joined into one. That's how concatenation works: the . operator adds strings together, creating a new string.

Gluing a string and a variable

If the variable $first holds the string "Kings", we can safely glue it together with another string:

<?php

$first = 'Kings';
$what = $first . 'road';
print_r($what); // => Kingsroad

PHP substitutes the value of the variable, performs the operation, and creates the resulting string.

Gluing two variables

In exactly the same way, you can combine the values of two variables if both contain strings:

<?php

$first = 'Kings';
$last = 'road';
$what = $first . $last;
print_r($what); // => Kingsroad

You can also add spaces:

$full = $first . ' ' . $last;
print_r($full); // => Kings road
$first = 'Kings'
$last  = 'road'

$first  .  ' '  .  $last
└─┬──┘            └──┬─┘
'Kings' .  ' '  .  'road'
└─────────┬────────────┘
     'Kings road'

What if a variable contains a number?

Let's try this:

<?php

$age = 42;
print_r('Age: ' . $age); // => Age: 42

In PHP this code works: the . operator always glues strings, so the number is automatically converted to a string. Not all languages behave this way — for example, in Python gluing a string and a number leads to an error, and the number has to be converted explicitly.

The same applies to variables with the results of calculations:

<?php

$price = 50 * 1.25 * 6.91; // => 431.875
print_r('Price in yuans: ' . $price); // => Price in yuans: 431.875

Automatic conversion is convenient, but it requires attention: sometimes it's not what you expect that gets turned into a string. We'll talk more about this behavior in the lessons about data types.

Instructions

An online store sends the customer an email after they place an order. The email is generated automatically based on the customer's data.

Write a program that generates a greeting and the body of the email using the ready-made variables, and prints the strings to the screen.

For the greeting, use the variables $firstName and $greeting, a comma, and an exclamation mark. For the body of the email, use the variables $intro and $info — the second sentence should be on a new line.

The result on the screen:

Здравствуйте, Анна!
Спасибо за ваш заказ.
Ожидаемая дата доставки — 3 рабочих дня.

Complete the task using only two print_r() calls. Don't forget that print_r() doesn't add a line break by itself — you need to add the \n break in the right places yourself.

Tips

  • Consider which string and in what order you need to concatenate the variables to get this two-line output of the email body.

  • Remember that you can create a string that contains only the escape sequence \n. You can concatenate this string with variables to format the text correctly.

If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:

  • Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Found a bug? Have something to add? Pull requests are welcome!