• Language Reference

Assignment Operators

The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

The value of an assignment expression is the value assigned. That is, the value of " $a = 3 " is 3. This allows you to do some tricky things: <?php $a = ( $b = 4 ) + 5 ; // $a is equal to 9 now, and $b has been set to 4. ?>

In addition to the basic assignment operator, there are "combined operators" for all of the binary arithmetic , array union and string operators that allow you to use a value in an expression and then set its value to the result of that expression. For example: <?php $a = 3 ; $a += 5 ; // sets $a to 8, as if we had said: $a = $a + 5; $b = "Hello " ; $b .= "There!" ; // sets $b to "Hello There!", just like $b = $b . "There!"; ?>

Note that the assignment copies the original variable to the new one (assignment by value), so changes to one will not affect the other. This may also have relevance if you need to copy something like a large array inside a tight loop.

An exception to the usual assignment by value behaviour within PHP occurs with object s, which are assigned by reference. Objects may be explicitly copied via the clone keyword.

Assignment by Reference

Assignment by reference is also supported, using the " $var = &$othervar; " syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere.

Example #1 Assigning by reference

The new operator returns a reference automatically, as such assigning the result of new by reference is an error.

The above example will output:

More information on references and their potential uses can be found in the References Explained section of the manual.

Arithmetic Assignment Operators

Example Equivalent Operation
$a += $b $a = $a + $b Addition
$a -= $b $a = $a - $b Subtraction
$a *= $b $a = $a * $b Multiplication
$a /= $b $a = $a / $b Division
$a %= $b $a = $a % $b Modulus
$a **= $b $a = $a ** $b Exponentiation

Bitwise Assignment Operators

Example Equivalent Operation
$a &= $b $a = $a & $b Bitwise And
$a |= $b $a = $a | $b Bitwise Or
$a ^= $b $a = $a ^ $b Bitwise Xor
$a <<= $b $a = $a << $b Left Shift
$a >>= $b $a = $a >> $b Right Shift

Other Assignment Operators

Example Equivalent Operation
$a .= $b $a = $a . $b String Concatenation
$a ??= $b $a = $a ?? $b Null Coalesce
  • arithmetic operators
  • bitwise operators
  • null coalescing operator

Improve This Page

User contributed notes 4 notes.

To Top

  • PHP Tutorial
  • PHP Exercises
  • PHP Calendar
  • PHP Filesystem
  • PHP Programs
  • PHP Array Programs
  • PHP String Programs
  • PHP Interview Questions
  • PHP IntlChar
  • PHP Image Processing
  • PHP Formatter
  • Web Technology

Concatenation of two strings in PHP

  • How to Concatenate Strings in PHP ?
  • String Concatenation in C++
  • How to Concatenate Strings in JavaScript ?
  • How to Concatenate Two Arrays in PHP ?
  • Concatenation of strings in PL/SQL
  • Concatenating Two Strings in C
  • Python - Concatenation of two String Tuples
  • String concatenation in Scala
  • Python String Concatenation
  • How to Concatenate Multiple Strings in C++?
  • String Concatenation in R Programming
  • How to Concatenate Multiple Strings in Java?
  • How to Concatenate Strings in Swift?
  • Shell Script to Concatenate Two Strings
  • Python - Horizontal Concatenation of Multiline Strings
  • Python - Triple quote String concatenation
  • Batch Script - String Concatenation
  • Concatenate Two Strings in R programming - paste() method
  • How to Append or Concatenate Strings in Dart?

There are two string operators. The first is the concatenation operator (‘ . ‘), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (‘ .= ‘), which appends the argument on the right side to the argument on the left side. 

Code #1:  

Time complexity : O(n)  Auxiliary Space : O(n)

  Code #2 : 

  Code #3 :  

  Code #4 :

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples .

Please Login to comment...

Similar reads.

  • Web Technologies

Improve your Coding Skills with Practice

 alt=

What kind of Experience do you want to share?

Home » PHP Tutorial » PHP Assignment Operators

PHP Assignment Operators

Summary : in this tutorial, you will learn about the most commonly used PHP assignment operators.

Introduction to the PHP assignment operator

PHP uses the = to represent the assignment operator. The following shows the syntax of the assignment operator:

On the left side of the assignment operator ( = ) is a variable to which you want to assign a value. And on the right side of the assignment operator ( = ) is a value or an expression.

When evaluating the assignment operator ( = ), PHP evaluates the expression on the right side first and assigns the result to the variable on the left side. For example:

In this example, we assigned 10 to $x, 20 to $y, and the sum of $x and $y to $total.

The assignment expression returns a value assigned, which is the result of the expression in this case:

It means that you can use multiple assignment operators in a single statement like this:

In this case, PHP evaluates the right-most expression first:

The variable $y is 20 .

The assignment expression $y = 20 returns 20 so PHP assigns 20 to $x . After the assignments, both $x and $y equal 20.

Arithmetic assignment operators

Sometimes, you want to increase a variable by a specific value. For example:

How it works.

  • First, $counter is set to 1 .
  • Then, increase the $counter by 1 and assign the result to the $counter .

After the assignments, the value of $counter is 2 .

PHP provides the arithmetic assignment operator += that can do the same but with a shorter code. For example:

The expression $counter += 1 is equivalent to the expression $counter = $counter + 1 .

Besides the += operator, PHP provides other arithmetic assignment operators. The following table illustrates all the arithmetic assignment operators:

OperatorExampleEquivalentOperation
+=$x += $y$x = $x + $yAddition
-=$x -= $y$x = $x – $ySubtraction
*=$x *= $y$x = $x * $yMultiplication
/=$x /= $y$x = $x / $yDivision
%=$x %= $y$x = $x % $yModulus
**=$z **= $y$x = $x ** $yExponentiation

Concatenation assignment operator

PHP uses the concatenation operator (.) to concatenate two strings. For example:

By using the concatenation assignment operator you can concatenate two strings and assigns the result string to a variable. For example:

  • Use PHP assignment operator ( = ) to assign a value to a variable. The assignment expression returns the value assigned.
  • Use arithmetic assignment operators to carry arithmetic operations and assign at the same time.
  • Use concatenation assignment operator ( .= )to concatenate strings and assign the result to a variable in a single statement.

CodedTag

  • PHP String Operators

In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and concatenating strings in PHP. This entails combining two or more strings. The concatenation assignment operator (.=) is particularly useful for appending the right operand to the left operand.

Let’s explore these operators in more detail:

Concatenation Operator (.)

The concatenation operator (.) is utilized to combine two strings. Here’s an example:

You can concatenate more than two strings by chaining multiple concatenation operations.

Let’s take a look at another pattern of the concatenation operator, specifically the concatenation assignment operator.

Concatenation Assignment Operator (.=)

The .= operator is a shorthand assignment operator that concatenates the right operand to the left operand and assigns the result to the left operand. This is particularly useful for building strings incrementally:

This is equivalent to $greeting = $greeting . " World!"; .

Let’s see some examples

Examples of Concatenating Strings in PHP

Here are some more advanced examples demonstrating the use of both the concatenation operator (.) and the concatenation assignment operator (.=) in PHP:

Concatenation Operator ( . ):

In this example, the . operator is used to concatenate multiple strings and variables into a single string.

Concatenation Assignment Operator ( .=) :

Here, the .= operator is used to append additional text to the existing string in the $paragraph variable. It is a convenient way to build up a string gradually.

Concatenation Within Iterations:

You can also use concatenation within iterations to build strings dynamically. Here’s an example using a loop to concatenate numbers from 1 to 5:

In this example, the .= operator is used within the for loop to concatenate the current number and a string to the existing $result string. The loop iterates from 1 to 5, building the final string. The rtrim function is then used to remove the trailing comma and space.

You can adapt this concept to various scenarios where you need to dynamically build strings within loops, such as constructing lists, sentences, or any other formatted output.

These examples showcase how you can use string concatenation operators in PHP to create more complex strings by combining variables, literals, iterations and other strings.

Let’s summarize it.

Wrapping Up

PHP provides powerful string operators that are essential for manipulating and concatenating strings. The primary concatenation operator (.) allows for the seamless combination of strings, while the concatenation assignment operator (.=) provides a convenient means of appending content to existing strings.

This versatility is demonstrated through various examples, including simple concatenation operations, the use of concatenation assignment for gradual string construction, and dynamic string building within iterations.

For more PHP tutorials, visit here or visit PHP Manual .

Did you find this article helpful?

 width=

Sorry about that. How can we improve it ?

  • Facebook -->
  • Twitter -->
  • Linked In -->
  • Install PHP
  • Hello World
  • PHP Constant
  • PHP Comments

PHP Functions

  • Parameters and Arguments
  • Anonymous Functions
  • Variable Function
  • Arrow Functions
  • Variadic Functions
  • Named Arguments
  • Callable Vs Callback
  • Variable Scope

Control Structures

  • If-else Block
  • Break Statement

PHP Operators

  • Operator Precedence
  • PHP Arithmetic Operators
  • Assignment Operators
  • PHP Bitwise Operators
  • PHP Comparison Operators
  • PHP Increment and Decrement Operator
  • PHP Logical Operators
  • Array Operators
  • Conditional Operators
  • Ternary Operator
  • PHP Enumerable
  • PHP NOT Operator
  • PHP OR Operator
  • PHP Spaceship Operator
  • AND Operator
  • Exclusive OR
  • Spread Operator
  • Null Coalescing Operator

Data Format and Types

  • PHP Data Types
  • PHP Type Juggling
  • PHP Type Casting
  • PHP strict_types
  • Type Hinting
  • PHP Boolean Type
  • PHP Iterable
  • PHP Resource
  • Associative Arrays
  • Multidimensional Array

String and Patterns

  • Remove the Last Char
  • How to Concatenate Strings in PHP

Use the Concatenation Operator to Concatenation Strings in PHP

Use the concatenation assignment operator to concatenate strings in php, use the sprintf() function to concatenate strings in php.

How to Concatenate Strings in PHP

This article will introduce different methods to perform string concatenation in PHP.

The process of joining two strings together is called the concatenation process. In PHP, we can achieve this by using the concatenation operator. The concatenation operator is . . The correct syntax to use this operator is as follows.

The details of these variables are as follows.

Variables Description
It is the string in which we will store the concatenated strings.
It is the string that we want to concatenate with the other string.
It is the string that we want to concatenate with the first string.

The program below shows how we can use the concatenation operator to combine two strings.

Likewise, we can use this operator to combine multiple strings.

In PHP, we can also use the concatenation assignment operator to concatenate strings. The concatenation assignment operator is .= . The difference between .= and . is that the concatenation assignment operator .= appends the string on the right side. The correct syntax to use this operator is as follows.

Variables Description
It is the string with which we want to append a new string on the right side.
It is the string that we want to concatenate with the first string.

The program below shows how we can use the concatenation assignment operator to combine two strings.

In PHP, we can also use the sprintf() function to concatenate strings. This function gives several formatting patterns to format strings. We can use this formatting to combine two strings. The correct syntax to use this function is as follows.

The function sprintf() accepts N+1 parameters. The detail of its parameters is as follows.

Parameters Description
mandatory The format will be applied to the given string or strings.
, , mandatory It is the string we want to format. At least one string is mandatory.

The function returns the formatted string. We will use the format %s %s to combine two strings. The program that combines two strings is as follows:

Related Article - PHP String

  • How to Remove All Spaces Out of a String in PHP
  • How to Convert DateTime to String in PHP
  • How to Convert String to Date and Date-Time in PHP
  • How to Convert an Integer Into a String in PHP
  • How to Convert an Array to a String in PHP
  • How to Convert a String to a Number in PHP

Concatenate Strings in PHP

Php examples tutorial index.

Concatenating strings is a crucial concept in PHP that enables developers to merge two or more strings to create a single string. Understanding how to concatenate strings efficiently is vital in generating dynamic content, creating queries, and managing data output. In this tutorial, you will learn how to concatenate strings in PHP.

Understanding String Concatenation in PHP

String concatenation refers to joining two or more strings to create a new string. It is a typical and helpful operation in PHP, allowing developers to create customized and dynamic strings by combining different sources. The dot ( . ) operator performs this operation in PHP by joining the strings and creating a new string. This feature is simple and efficient and is commonly used in PHP scripting.

Basic Concatenation with the Dot Operator

The easiest and most common way to concatenate strings in PHP is to use the dot ( . ) operator. The dot operator takes two operands (the strings to be concatenated) and returns a new string as the result of concatenating them.

In the above example, we combine two variables ( $firstName and $lastName ) with a space between them to form a full name.

Concatenation with Assignment Operator

PHP makes string manipulation easier with the concatenation assignment operator ( .= ). This operator appends the right-side argument to the left-side argument, making it simple to expand an existing string.

The above method simplifies how to efficiently append text to an existing string variable, enhancing its content without redundancy.

Concatenating Multiple Variables and Strings

You can concatenate multiple variables and strings using multiple dot operators in a single statement. It is beneficial when you need to create a sentence or message from different data sources:

Dynamic Content Creation

String concatenation helps generate dynamic content and allows tailored user experiences based on specific inputs or conditions.

The above example illustrates how to create a personalized greeting message using concatenation dynamically.

In this tutorial, you have learned about the process of string concatenation in PHP, how to use the dot and assignment operators to join strings together, and some examples of string concatenation in PHP for different purposes. String concatenation is a crucial aspect of managing dynamic content and data output. Applying the best practices and techniques outlined in this tutorial, you can efficiently use string concatenation in your PHP projects.

String Operators in PHP : Tutorial

Concatenation Operator

Concatenation assignment operator (.=).

Primary links

  • HTML Color Chart
  • Knowledge Base
  • Devguru Resume
  • Testimonials
  • Privacy Policy

Font-size: A A A

PHP » Operators » .=

Concatenation assignment operator.

Concatenates the left operand and the right operand.

Explanation:

A variable is changed with an assignment operator.

concatenation assignment operator php

© Copyright 1999-2018 by Infinite Software Solutions, Inc. All rights reserved.

Trademark Information | Privacy Policy

Event Management Software and Association Management Software

Concatenating Strings in PHP

What is php.

PHP , an open-source scripting language widely used for web development, can be integrated into HTML code, where PHP and HTML syntax constructs can be mixed in one file. PHP is known for its flexibility and large developer community. PHP supports multiple databases such as MySQL , PostgreSQL , and SQLite . PHP can run on many operating systems, including Windows, Linux, and macOS.

What is a string in PHP?

In PHP , a string is a sequence of characters, representing each character by a byte. Due to the limited range of a byte (256 characters), PHP strings do not support Unicode strings. However, PHP offers several ways to work with Unicode strings. You can create PHP strings using single quotes ('...'), double quotes ("..."), and Heredoc syntax (<<<). PHP provides many built-in string functions for various operations such as comparison , replacement , interpolation , splitting , etc.

PHP Concatenate Strings Examples

The following are examples of string concatenation in PHP:

Concatenating strings using concatenate operator (' . ')

To concatenate strings in PHP, you can use the concatenation operator (' . '), which will concatenate the strings and assign the result to a variable.

Concatenating strings using the assignment operator (' .= ')

To concatenate strings, you can aloso use the concatenation assignment operator (' .= '), which adds the argument on the right side to the argument on the left side.

  • How do I send a GET request using PHP?
  • How do I POST JSON data in PHP using Curl?
  • How do I send a POST request using PHP?

PHP String Concatenation Related API examples and articles

Why sign up.

  • Save your projects in the cloud
  • Manage shared requests
  • Increased rate limits

Are you sure you want to delete the item? All existing links to this item will no longer work.

Your request has been shared

Copy & share this link wherever you want.

Reset Password

Embed reqbin widget into your website, copy & paste snippet code, this location is available in a premium plan, see premium plans, join reqbin closed beta, edit article, reqbin google chrome extension.

Add the ReqBin Google Chrome Extension to your browser to send requests to the localhost and servers on your local network.

Ask Public Question (Beta)

How can we improve it, create redirect, redirect share, create/edit subscription, what do you use reqbin for the most.

Know the Code

Developing & Empowering WordPress Developers

Unlock your potential with a Pro Membership

Here is where you propel your career forward. Come join us today. Learn more.

Login to your account

PHP 101: Concatenating Assignment Operator

Lab: wordpress custom taxonomy basics.

Video Runtime: 02:26

The term “concatenating” means that we are smooshing two strings together by appending the one on the right to the one on the left. The result is a new string value.

For example, let’s say that a variable $post_meta has a value of '[post_categories] [post_tags]' . You want to append another shortcode to the end of it. How do you do that?

There are two different ways to achieve this and make an assignment.

Long Hand Approach

To concatenate an existing variable’s string value to some value you are processing, you have a couple of choices in PHP. You can do it with the long hand approach, like this:

$post_meta = $post_meta . ' [post_terms taxonomy="department"]';

where the shortcode’s string literal is smooshed together to the end of the string value in the variable $post_meta . Then the new string is assigned back to the variable. That’s the long hand version.

Short Hand Approach

PHP provides you with a concatenating assignment operator as a shorthand approach:

$post_meta .= ‘ [post_terms taxonomy=”department”]’;

This code works the same as the full version; however, it’s more condensed. It eliminates the repetitious repeating of the variable. Therefore, this approach is more readable and maintainable.

This approach is very popular and prevalent! Make sure you understand it completely!

What’s the Sequence and Result?

Let’s walk through the processing and look at the result.

Step 1: Concatenate

First the variable’s value and string literal are concatenated to form a new string value of:

'[post_categories] [post_tags] [post_terms taxonomy="department"]';

Step 2: Assignment

The next step is to assign the new string to the variable $post_meta , which means the value it represents is changed to:

$post_meta = '[post_categories] [post_tags] [post_terms taxonomy="department"]';

This Episode

In this episode, let’s talk about the process of concatenating. Then we’ll walk through how each of these approaches works. Finally, we’ll see the results.

Code Challenge

Let’s challenge you. Ready? When this function is called, a string literal of '[post_categories]' is passed to it and assigned to the parameter $html . What is the value returned when the function is done running?

function filter_the_entry_footer_post_meta( $html ) {
$html .= ' [post_terms taxonomy="department"]';
return $html;
}
$content = filter_the_entry_footer_post_meta( '[post_categories]' );
// what is the value of $content?

Answer : '[post_categories] [post_terms taxonomy="department"]'

Why? PHP concatenates the incoming value with the string literal and then assigns it back to the variable. Then that variable’s value is returned.

Did you get it right? If yes, way to go!! If no, watch the video and if it still doesn’t make sense, come ask me in the Pro Forums.

You get WET when you swim. Stay DRY when you code.

Total Lab Runtime: 01:30:53

  • 1 Lab Introduction free 08:15
  • 2 Custom Taxonomy - The What, Why, and When free 08:32
  • 3 Registering a Custom Taxonomy pro 09:54
  • 4 Configure the Labels pro 14:51
  • 5 Bind to Post Types pro 11:55
  • 6 Configuring Arguments pro 07:52
  • 7 Render Entry Footer Terms pro 08:40
  • 8 PHP 101: Concatenating Assignment Operator pro 02:26
  • 9 PHP 101: Building Strings pro 08:19
  • 10 Test Entry Footer Terms pro 03:03
  • 11 Flush Rewrite Rules pro 03:56
  • 12 Wrap it Up pro 03:10

Developing Professional WordPress Developers - Know the Code

Know the Code develops and empowers professional WordPress developers, like you. We help you to grow, innovate, and prosper.

  • Mastery Libraries
  • What’s New
  • Help Center
  • Developer Stories
  • My Dashboard
  • WP Developers’ Club

Know the Code flies on WP Engine . Check out the managed hosting solutions from WP Engine.

WordPress® and its related trademarks are registered trademarks of the WordPress Foundation. The Genesis framework and its related trademarks are registered trademarks of StudioPress. This website is not affiliated with or sponsored by Automattic, Inc., the WordPress Foundation, or the WordPress® Open Source Project.

  • ▼PHP Operators
  • Arithmetic Operators
  • Comparison Operators
  • Logical Operators
  • Assignment Operators
  • Bitwise Operators

String Operators

  • Array Operators
  • Incrementing Decrementing Operators

PHP: String operator

There are two string operators : concatenation operator ('.') and concatenating assignment operator ('.=').

Example : PHP string concatenation operator

View the example in the browser

Example: PHP string concatenating assignment operator

View the example of in the browser

Previous: Bitwise Operators Next: Array Operators

Follow us on Facebook and Twitter for latest update.

  • Weekly Trends and Language Statistics

Javatpoint Logo

PHP Tutorial

Control statement, php programs, php functions, php strings, php include, state management, upload download, php oops concepts, related tutorials.

Interview Questions

JavaTpoint

In this article, we will create a string concatenation in PHP. In this, we will learn the basics of PHP string. After that we will learn this concept with the help of various examples.

A string is a collection of characters. It offers various types of string operators and these operators have different functionalities such as string concatenation, compare values and to perform Boolean operations.

In , this operator is used to combine the two string values and returns it as a new string.

In the above example, we have concatenated the two strings in the third string. In this we have taken two variables $a and $b as a string type and $c variable is also string type in which the concatenate string is stored.

For this purpose we have used the Concatenation operator (.) to concatenate the strings.

Below figure demonstrates the output of this example:

In the above example, we have concatenated the two strings in the third string. In this we have taken two variables $fname and $lname as a string type and $c variable is also string type in which the concatenate string is stored.

For this purpose we have used the Concatenation operator (.) to concatenate the strings.

Below figure demonstrates the output of this example:

This operation appends the argument on the right side to the argument on the left side.

In the above example, we have concatenated the two strings in one string variable. In this we have taken a variable $string1 in which the concatenate string is stored. For this purpose we have used the Concatenation Assignment operator (".=") to concatenate the strings.

Below figure demonstrates the output of this example:

In the above example, we have concatenated the variable in the array string. In this we have taken a variable $a and & b string array in which the concatenate string is stored. For this purpose we have used the Concatenation Assignment operator (".=") to concatenate the strings.

Below figure demonstrates the output of this example:





Youtube

  • Send your Feedback to [email protected]

Help Others, Please Share

facebook

Learn Latest Tutorials

Splunk tutorial

Transact-SQL

Tumblr tutorial

Reinforcement Learning

R Programming tutorial

R Programming

RxJS tutorial

React Native

Python Design Patterns

Python Design Patterns

Python Pillow tutorial

Python Pillow

Python Turtle tutorial

Python Turtle

Keras tutorial

Preparation

Aptitude

Verbal Ability

Interview Questions

Company Questions

Trending Technologies

Artificial Intelligence

Artificial Intelligence

AWS Tutorial

Cloud Computing

Hadoop tutorial

Data Science

Angular 7 Tutorial

Machine Learning

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures

DAA tutorial

Operating System

Computer Network tutorial

Computer Network

Compiler Design tutorial

Compiler Design

Computer Organization and Architecture

Computer Organization

Discrete Mathematics Tutorial

Discrete Mathematics

Ethical Hacking

Ethical Hacking

Computer Graphics Tutorial

Computer Graphics

Software Engineering

Software Engineering

html tutorial

Web Technology

Cyber Security tutorial

Cyber Security

Automata Tutorial

C Programming

C++ tutorial

Control System

Data Mining Tutorial

Data Mining

Data Warehouse Tutorial

Data Warehouse

RSS Feed

PHP Tutorial

Php advanced, mysql database, php examples, php reference, php operators.

Operators are used to perform operations on variables and values.

PHP divides the operators in the following groups:

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Increment/Decrement operators
  • Logical operators
  • String operators
  • Array operators
  • Conditional assignment operators

PHP Arithmetic Operators

The PHP arithmetic operators are used with numeric values to perform common arithmetical operations, such as addition, subtraction, multiplication etc.

Operator Name Example Result Try it
+ Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power

PHP Assignment Operators

The PHP assignment operators are used with numeric values to write a value to a variable.

The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

Assignment Same as... Description Try it
x = y x = y The left operand gets set to the value of the expression on the right
x += y x = x + y Addition
x -= y x = x - y Subtraction
x *= y x = x * y Multiplication
x /= y x = x / y Division
x %= y x = x % y Modulus

Advertisement

PHP Comparison Operators

The PHP comparison operators are used to compare two values (number or string):

Operator Name Example Result Try it
== Equal $x == $y Returns true if $x is equal to $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.

PHP Increment / Decrement Operators

The PHP increment operators are used to increment a variable's value.

The PHP decrement operators are used to decrement a variable's value.

Operator Same as... Description Try it
++$x Pre-increment Increments $x by one, then returns $x
$x++ Post-increment Returns $x, then increments $x by one
--$x Pre-decrement Decrements $x by one, then returns $x
$x-- Post-decrement Returns $x, then decrements $x by one

PHP Logical Operators

The PHP logical operators are used to combine conditional statements.

Operator Name Example Result Try it
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

PHP String Operators

PHP has two operators that are specially designed for strings.

Operator Name Example Result Try it
. Concatenation $txt1 . $txt2 Concatenation of $txt1 and $txt2
.= Concatenation assignment $txt1 .= $txt2 Appends $txt2 to $txt1

PHP Array Operators

The PHP array operators are used to compare arrays.

Operator Name Example Result Try it
+ Union $x + $y Union of $x and $y
== Equality $x == $y Returns true if $x and $y have the same key/value pairs
=== Identity $x === $y Returns true if $x and $y have the same key/value pairs in the same order and of the same types
!= Inequality $x != $y Returns true if $x is not equal to $y
<> Inequality $x <> $y Returns true if $x is not equal to $y
!== Non-identity $x !== $y Returns true if $x is not identical to $y

PHP Conditional Assignment Operators

The PHP conditional assignment operators are used to set a value depending on conditions:

Operator Name Example Result Try it
?: Ternary $x = ? : Returns the value of $x.
The value of $x is if = TRUE.
The value of $x is if = FALSE
?? Null coalescing $x = ?? Returns the value of $x.
The value of $x is if exists, and is not NULL.
If does not exist, or is NULL, the value of $x is .
Introduced in PHP 7

PHP Exercises

Test yourself with exercises.

Multiply 10 with 5 , and output the result.

Start the Exercise

Get Certified

COLOR PICKER

colorpicker

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: [email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail: [email protected]

Top Tutorials

Top references, top examples, get certified.

  • Stack Overflow Public questions & answers
  • Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers
  • Talent Build your employer brand
  • Advertising Reach developers & technologists worldwide
  • Labs The future of collective knowledge sharing
  • About the company

Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Get early access and see previews of new features.

Concatante PHP variables inside loop by a "Concatenating Assignment Operator", and Echo each output loop separately

With the "Concatenating Assignment Operator" I assigned in the loop two variables. I need to get each loop result separately. The problem is that I don't know why each next loop result is copied to each next loop result.

Using this code, I get the output:

I'm working on making the output look like this:

Grzes's user avatar

  • 7 You need to reset $output at the top of the loop. –  aynber Commented Dec 1, 2021 at 14:14

You are always concatenating values to the $output, you never clear it, so the numbers are just continually added. All you need to do is change the first $output .= "1"; in to a $output = "1"; and that will have the effect of resetting $output to the one character ready to be concatenated with the second.

RiggsFolly's user avatar

Your Answer

Reminder: Answers generated by artificial intelligence tools are not allowed on Stack Overflow. Learn more

Sign up or log in

Post as a guest.

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy .

Not the answer you're looking for? Browse other questions tagged php arrays while-loop or ask your own question .

  • Featured on Meta
  • Upcoming sign-up experiments related to tags
  • The 2024 Developer Survey Is Live
  • Policy: Generative AI (e.g., ChatGPT) is banned
  • The return of Staging Ground to Stack Overflow

Hot Network Questions

  • How can one be a monergist and deny irresistible grace?
  • How can you destroy a mage hand?
  • Looking for a story that possibly started MYOB
  • Split Flaps and lift?
  • How would wyrms develop culture, houses, etc?
  • Is a possessive apostrophe appropriate in the verb phrase 'to save someone something'?
  • Structure of a single automorphism of a finite abelian p-group
  • Why is “selling a birthright (πρωτοτόκια)” so bad? -- Hebrews 12:16
  • Did the NES CPU save die area by omitting BCD?
  • What might cause an inner tube to "behave" flat in a tire?
  • Create sublists whose totals exceed a certain threshold and that are as short as possible
  • What is the history and meaning of letters “v” and “e” in expressions +ve and -ve?
  • Why are heavy metals toxic? Lead and Carbon are in the same group. One is toxic, the other is not
  • Short story in which the main character buys a robot psychotherapist to get rid of the obsessive desire to kill
  • Surfing social media is one of the most, if not the most popular ______ among my friends => pastime or pastimes?
  • What was the title and author of this children's book of mazes?
  • How much time is needed to judge an Earth-like planet to be safe?
  • How to count the number of lines in an array
  • Formal language Concatenation is a binary operation?
  • "comfortable", but in the conceptual sense
  • Are there any precautions I should take if I plan on storing something very heavy near my foundation?
  • Protocol Used by the \oldstylenums Command to Display Digits
  • A Fantasy movie with a powerful humanoid being that lives in water
  • In general, How's a computer science subject taught in Best Universities of the World that are not MIT level?

concatenation assignment operator php

IMAGES

  1. PHP Tutorial For Beginners 12

    concatenation assignment operator php

  2. PHP String Concatenation Example With Demo

    concatenation assignment operator php

  3. #6 Concatenate in PHP

    concatenation assignment operator php

  4. PHP tutorial

    concatenation assignment operator php

  5. PHP Concatenation Operators

    concatenation assignment operator php

  6. Concatenation And Concatenation Assignment Operator In PHP

    concatenation assignment operator php

VIDEO

  1. 035 Action Time Concatenation Operator

  2. Php tutorials in hindi English

  3. Concatenation Operator & Using Literal Character String

  4. Combine Text and Values with the '&' Operator for Powerful Concatenation! #excel #text #msoffice

  5. PHP lesson 9

  6. Lecture-24| String Concatenation Using Plus Operator Overloading in C++ in Pashto

COMMENTS

  1. PHP: String

    There are two string operators. The first is the concatenation operator ('.'), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (' .= '), which appends the argument on the right side to the argument on the left side. Please read Assignment Operators for more information.

  2. PHP Concatenation Operators

    PHP Compensation Operator is used to combine character strings. Operator. Description. . The PHP concatenation operator (.) is used to combine two string values to create one string. .=. Concatenation assignment.

  3. PHP: Assignment

    Assignment Operators. The basic assignment operator is "=". Your first inclination might be to think of this as "equal to". Don't. It really means that the left operand gets set to the value of the expression on the right (that is, "gets set to").

  4. PHP

    String Concatenation. To concatenate, or combine, two strings you can use the . operator:

  5. Concatenation of two strings in PHP

    There are two string operators. The first is the concatenation operator ('. '), which returns the concatenation of its right and left arguments. The second is the concatenating assignment operator (' .= '), which appends the argument on the right side to the argument on the left side. Examples : string2 : World! Output : HelloWorld!

  6. Can I initialize a PHP variable with a concat assignment operator?

    Will initializing a PHP variable with the concatenation assignment operator (.=) cause problems? I'm in a situation where a variable may or may not have already been created, but if it is, I don't want to overwrite it.

  7. PHP Assignment Operators

    Use PHP assignment operator ( =) to assign a value to a variable. The assignment expression returns the value assigned. Use arithmetic assignment operators to carry arithmetic operations and assign at the same time. Use concatenation assignment operator ( .= )to concatenate strings and assign the result to a variable in a single statement.

  8. Concatenating Strings in PHP: Tips and Examples

    In PHP, string operators, such as the concatenation operator (.) and its assignment variant (.=), are employed for manipulating and concatenating strings in PHP. This entails combining two or more strings. The concatenation assignment operator (.=) is particularly useful for appending the right operand to the left operand.

  9. How to Concatenate Strings in PHP

    Use the Concatenation Assignment Operator to Concatenate Strings in PHP. In PHP, we can also use the concatenation assignment operator to concatenate strings. The concatenation assignment operator is .=. The difference between .= and . is that the concatenation assignment operator .= appends the string on the right side. The correct syntax to ...

  10. Learn to Concatenate Strings in PHP

    String concatenation refers to joining two or more strings to create a new string. It is a typical and helpful operation in PHP, allowing developers to create customized and dynamic strings by combining different sources. The dot (.) operator performs this operation in PHP by joining the strings and creating a new string.

  11. PHP String Operators(Concatenation) Tutorial : Code2care

    There are two String operators in PHP. 1. Concatenation Operator "." (dot) 2. Concatenation Assignment Operator ".=" (dot equals) Concatenation Operator Concatenation is the operation of joining two character strings/variables together. In PHP we use . (dot) to join two strings or variables. Below are some examples of string concatenation:

  12. PHP >> Operators >> .=

    PHP » Operators » .= Syntax: $var .= expressionvarA variable.expressionA value to concatenate the variable with.Concatenation assignment operator.

  13. How do I concatenate strings in PHP?

    The assignment operator (' .= ') adds the argument on the right side to the argument on the left side. This is a shorter way to concatenate the arguments and assign the result to the same variable. In this PHP String Concatenation example, we use the concatenation operator (' . ') and return the concatenating arguments. Click Execute to run the ...

  14. PHP 101: Concatenating Assignment Operator

    PHP 101: Concatenating Assignment Operator Lab: WordPress Custom Taxonomy Basics. Video Runtime: 02:26. ... There are two different ways to achieve this and make an assignment. Long Hand Approach. To concatenate an existing variable's string value to some value you are processing, you have a couple of choices in PHP. ...

  15. php

    3. Only slightly, since PHP has to parse the entire string looking for variables, while with concatenation, it just slaps the two variables together. So there's a tiny performance hit, but it's not noticeable for most things. It's a lot easier to concatenate variables like $_SERVER['DOCUMENT_ROOT'] using the concatenation operator (with quotes ...

  16. What is the .= (dot equals) operator in PHP?

    Your question is about the operator .=.It is a shorthand to a string concatenation followed by an assignment.. On assigment by operation operators. There is a family of operators we can call assignment by xyz, where xyz here represents a binary operation on operands of the same type, such as addition, subtraction, concatenation.. So, let's say we have an operator ⊕: int*int → int, meaning ...

  17. PHP: String operator

    String Operators. There are two string operators : concatenation operator ('.') and concatenating assignment operator ('.='). Example : PHP string concatenation operator

  18. PHP string Concatenation

    There are two types of string operators provided by PHP. 1. Concatenation Operator ("."): In PHP, this operator is used to combine the two string values and returns it as a new string. ... Let's take the various examples of how to use the Concatenation Assignment Operator (".=") to concatenate the strings in PHP. ...

  19. PHP Operators

    PHP Assignment Operators. The PHP assignment operators are used with numeric values to write a value to a variable. The basic assignment operator in PHP is "=". It means that the left operand gets set to the value of the assignment expression on the right.

  20. PHP Concatenation assignment

    PHP Concatenation assignment. Ask Question Asked 7 years, 11 months ago. Modified 7 years, 11 months ago. Viewed 456 times ... How do I use the .= operator within a loop, and any direction to tutorials to better understand this would be appreciated? php; Share. Improve this question.

  21. Concatante PHP variables inside loop by a "Concatenating Assignment

    With the "Concatenating Assignment Operator" I assigned in the loop two variables. I need to get each loop result separately. The problem is that I don't know why each next loop result is copied to each next loop result. ... Concatenate array on loop php. 0. Handling loops and combining values in PHP. Hot Network Questions Help me unlock an old ...