Concatenation in PHP

Concatenate is to unite, or paste elements that were separated. The elements will be, in our case, the texts, the variables and the constants that we need to process.

Concatenation in PHP | Learn PHP & MySQL | Concatenate is to unite, or paste elements that were separated. The elements will be, in our case, the texts, the variables and the constants that we need to process

In the previous example ($simpleVariables), we have encountered a very common problem: having a text wrapped in single quotes, and needing the variables included in it to be replaced by their value.

Other times, when dictating to the PHP interpreter long blocks of HTML code (with abundant double quotes included), for convenience we will choose to delimit the start and end in single quotes, and that will become an obstacle if we want to include data from variables between medium of that text.

Let's see why

<?php
$site='PHP 7';
$concatenation= '<h1 class="highlighted"> Welcome to $ site </h1>';
?>

This example will not produce the desired result, but this:

<?php class="highlighted">Welcome to $site</h1>

To solve it and get that variable replaced by its value, we will use a technique called concatenation.

Concatenating is uniting, it is "pasting" elements that were separated. These elements will be, in our case, the texts, the variables and the constants that we need to process.

We will base ourselves on the simple idea of "taking out" from the quotes the variables and constants that we want to replace by their value. That is, we will momentarily interrupt the block of text delimited by quotation marks and, after the variable, we will restart it.

For this task, that is, to end and recommend sections of text, we will use the concatenation operator, which is nothing more than a simple point “.”

That point will be the "glue" that will unite everything that suits us to keep safe from the quotation marks of the block, but keeping it attached to it. Let's see some examples to understand it better:

  1. Between a text and a variable:

<?php
$name='Pepe';
$concatenation='<p id="greeting">Hello '.$name.'</p>';
?>

Let's visualize better the three "sections" that make up this union of parts, looking at each part separately:

1 ‘<p id=”greeting”>Hello’
2 $name
3 ‘<p>’;

Those are the three sections that we are asking to be written (or rather in this case, to be stored within the variable called $ concatenation). First the opening of the paragraph label with its identifier that uses double quotes to wrap its value will be written, then this is added to the value that has the variable $ name (Pepe, in this case) and, finally, the label is closed. paragraph.

That code will produce this result:

<p id="greeting">Hello Pepe</p>

That was what we wanted to achieve.

  1. Between one variable and another variable:

<?php
$name='John';
$lasname='Perez';
$concatenacion='<p>Your first and last name is '.$nombre.$lastname.'</p>';
?>

We see that not only can a text be linked to a variable, but also two variables to each other, although this generated a small visual detail: the name and surname will be "stuck" with each other, since we have not included any space between both variables:

<p>Your name and surname are JohnPerez </p>

We can create a concatenated stretch of text, exclusively to insert that missing space:

<?php
$name='John';
$lastname='Perez';
$concatenacion='<p>Your first and last name is '.$name.' '.$lastname.'</p>';
?>

Note that the first stretch of text goes from the opening of the paragraph label to the end of the word "is"; the second section is the variable $ name; then there is a stretch of text in quotation marks that is only for writing a space; then follows the variable $ last name and, finally, the closing of the paragraph.

Before terminating this instruction to the use of variables and their combination with other texts, let us mention that the same variable, at different times, can change its stored value, as many times as possible.

For example:

<?php
$price=100;
$amount=5;
$total=$price*$amount;
/* $total contains 500 */
$increase=2;
$total=$total+$increase;
/* $total now contains 502 */
?>

While at it, let's observe that the order of execution of the terms of an equality (assignment) is always from right to left.

In this expression:

$total=$total+$increase;

First, the term that is to the right of the equal sign was processed; that is, the PHP interpreter made the replacement of these variables by their values:

$total+$increase;

That, in this case, was equal to:
500 + 2

Or what is the same:
502

Once that operation was completed and the elements of the term on the right reduced to a single value, that value was assigned as the new value of the variable $ total, by means of the sign =, which is the assignment operator; what would be the same as simplifying the entire term right already solved in this way:

$total = 502;

Thus, not only have we seen that the same variable can be reused, but we have also visualized how the PHP code is processed, step by step, before assigning value to a variable.

🤖

ChatGPT Free
Ask questions on any topic

CITE ARTICLE


For homework, research, thesis, books, magazines, blogs or academic articles

APA Format Reference:

Delgado, Hugo. (2019).
Concatenation in PHP.
Retrieved Nov 06, 2024, from
https://disenowebakus.net/en/php-concatenation

Participates!

Share it with your friends in Social Networks!

Professor at the University of Guadalajara

Hugo Delgado Desarrollador y Diseñador Web en Puerto Vallarta

Professional in Web Development and SEO Positioning for more than 10 continuous years.
We have more than 200 certificates and recognitions in the Academic and Professional trajectory, including diploma certificates certified by Google.

CONTINUE LEARNING

IT ALSO DESERVES TO PAY TO VISIT:

Not finding what you need?

Use our internal search to discover more information
Related content:

Would you like to learn more about Web Design?

Meet all the courses and tutorials that we have for you completely free
Learn Web Design
 

Leave your Comment

SPONSOR

Your business can also appear here. More information

Next article: