PHP String Operators

WBOY
Release: 2024-08-29 12:39:38
Original
472 people have browsed it

The following article provides an outline for PHP String Operators. The symbol element that perform any specific operation on string operand(s) is called a string operator instead of using any predefined method in the code. These string operators can be executed on both static as well as dynamic variables.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of PHP String Operators

PHP scripting language supports two types of string operation to be implemented using operators:

  • Concatenation Operator
  • Concatenating Assignment Operator

1. Concatenation Operator

This PHP string operator is used to perform concatenation operation on its immediate left and immediate right operands.

In PHP, (‘.’) (the dot) is used as concatenation operator.

The operator requires two operands to perform the operation.

Example #1

Code:

<?php
$input_left = "Application of concatenation Operator!";
$txt_right = " when both operands are static variables";
$Output = $input_left . $txt_right ;
echo "Left operand value: " ;
echo "\n";
echo $input_left;
echo "\n";
echo "\n";
echo "Right operand value: " ;
echo "\n";
echo $txt_right;
echo "\n";
echo "\n";
echo "Ouput from concatenation operator: " ;
echo "\n";
echo $Output;
?>
Copy after login

Output:

PHP String Operators

Example #2

Concatenation operator with more than two operands.

In case of multiple operands, the execution happens from left to right. First two left most operands get concatenated and then third operand gets embedded to the resultant string and operation continues till the right most operand is concatenated.

The operator to perform concatenation operation on 3 operands in one single command.

Code:

<?php
echo "Application of concatenation Operator on multiple Operands:";
echo "\n";
echo "\n";
$input1 = " First Operand--";
$input2 = " Second Operand--";
$input3 = " Third Operand ";
$Output = $input1 . $input2 . $input3 ;
echo "First operand value: " ;
echo "\n";
echo $input1;
echo "\n";
echo "\n";
echo "Second operand value: " ;
echo "\n";
echo $input2;
echo "\n";
echo "\n";
echo "Third operand value: " ;
echo "\n";
echo $input3;
echo "\n";
echo "\n";
echo "Ouput from concatenation operator: " ;
echo "\n>";
echo $Output;
?>
Copy after login

Output:

First and second operand value is concatenated and then third operand value is concatenated to the resultant string the first two operands.

PHP String Operators

2. Concatenating Assignment Operator

This PHP string operator is used to perform concatenation operation on its immediate left and immediate right operands and appends the result to the immediate right side operand.

In PHP, (‘.=’) (the dot(.)+Equals(=)) is used as Concatenating Assignment Operator.

This operator exhibits the concatenation operation on two operands and assigns the resultant value to left operand.

Example #1

Code:

<?php
echo "Application of Concatenating Assignment Operator two operands:";
echo "\n";
echo "\n";
$input_left = " Left Operand--";
$input_right = " Right Operand--";
echo "Before Concatenating Assignment Operator is called:";
echo "\n";
echo "\n";
echo "Left operand value: " ;
echo "\n";
echo $input_left;
echo "\n";
echo "\n";
echo "Right operand value: " ;
echo "\n";
echo $input_right;
echo "\n";
echo "\n";
$input_left .= $input_right;
echo "After Concatenating Assignment Operator is called:";
echo "\n";
echo "\n";
echo "Left operand value: " ;
echo "\n";
echo $input_left;
echo "\n";
echo "\n";
echo "Right operand value: " ;
echo "\n";
echo $input_right;
?>
Copy after login

Output:

PHP String Operators

Example #2

Concatenation operator with more than two operands.

In case of multiple operands, the execution happens from right to left. The two right most operands get concatenated and the result gets assigned to the left most operator out of those two operands and the operation continues till the final concatenation operation is occurred and the resultant value is assigned to left most operand of the complete command.

Code:

<?php
echo "Application of Concatenating Assignment Operator on multiple Operands:";
echo "\n";
echo "\n";
$input1 = " First Operand--";
$input2 = " Second Operand--";
$input3 = " Third Operand ";
echo "Before Concatenating Assignment Operator is called:";
echo "\n";
echo "\n";
echo "First operand value: " ;
echo "\n";
echo $input1;
echo "\n";
echo "\n";
echo "Second operand value: " ;
echo "\n";
echo $input2;
echo "\n";
echo "\n";
echo "Third operand value: " ;
echo "\n";
echo $input3;
echo "\n";
echo "\n";
$input1 .= $input2 .= $input3 ;
echo "After Concatenating Assignment Operator is called:";
echo "\n";
echo "\n";
echo "First operand value: " ;
echo "\n";
echo $input1;
echo "\n";
echo "\n";
echo "Second operand value: " ;
echo "\n";
echo $input2;
echo "\n";
echo "\n";
echo "Third operand value: " ;
echo "\n";
echo $input3;
?>
Copy after login

Output:

PHP String Operators

Additional Note

1. The dot operator possess same precedence as the arithmetic operators ‘+’ and ‘-‘. Hence while using those operators together may end in causing unexpected result as demonstrated in the below example.

Code:

<?php
$Intvar = 5;
echo "Result: " . $Intvar + 10;
?>
Copy after login

Output:

PHP String Operators

  • In order to avoid this issue, it is recommended to use parentheses.

Code:

<?php
$Intvar = 5;
echo "Result: " .( $Intvar + 10);
?>
Copy after login

Output:

PHP String Operators

  • But in the case of concatenating assignment operator, the issue does not occur.

Example:

Using concatenating assignment operator without parentheses:

Code:

<?php
$Intvar = 5;
$strvar="Result from concatenating assignment operator: ";
echo $strvar .= $Intvar + 10;
?>
Copy after login

Output:

PHP String Operators

2. If any of the string operand is happened to have empty or null value, both operators return the non-empty string operand value as output, do not throw any null value exception.

3. Both string operators are compatible to be used with both static as well as dynamic variables.

4. Any PHP string variable can be extended upto 2GB i.e. 2147483647 Byte as its default maximum limit. If the limit size is not overwritten by increasing limit for memory limit directive in php.ini and the resultant value from any of the string operand crosses the maximum limit, it may result in memory exception error.

5. In order to avoid the condition, include validation condition to restrict the output string to its safe limit.

The above is the detailed content of PHP String Operators. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template