PHP String Operators

In this chapter you will learn:

  1. Description
  2. Example - Use string operator to connect two strings

Description

There are only two string operators in PHP: concatenation and shorthand concatenation. Both are shown in the following table.

Operator Name Description
. Concatenation Returns the second value appended to the first: $a . $b
.= Shorthand concatenation Appends the second value to the first: $a .= $b

Example

These operators are used to join strings together, like this:


<?PHP/* ja v  a 2 s .  c o  m*/
      $first = "Hello, "; 
      $second = "world! from java2s.com"; 

      // join $first and $second; assign to $third 
      $third = $first . $second; 
      // $third is now "Hello, world!" 

      $first .= " officer!"; 
      // $first is now "Hello, officer!" 
?>

Next chapter...

What you will learn in the next chapter:

  1. What is Ternary Operator
  2. Syntax for PHP Ternary Operator
  3. Note for PHP Ternary Operator
  4. Example - Convert between ternary operator and if statement
Home » PHP Tutorial » PHP Operators
PHP Assignment Operators
PHP Arithmetic Operators
PHP Incrementing and Decrementing Operators
PHP Comparison Operators
PHP Logical Operators
PHP String Operators
PHP Ternary Operator
PHP Bitwise Operators
PHP Execution Operator
PHP Operator Precedence