Home >Backend Development >PHP Tutorial >PHP String Operators
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
PHP scripting language supports two types of string operation to be implemented using operators:
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.
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; ?>
Output:
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; ?>
Output:
First and second operand value is concatenated and then third operand value is concatenated to the resultant string the first two operands.
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.
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; ?>
Output:
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; ?>
Output:
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; ?>
Output:
Code:
<?php $Intvar = 5; echo "Result: " .( $Intvar + 10); ?>
Output:
Example:
Using concatenating assignment operator without parentheses:
Code:
<?php $Intvar = 5; $strvar="Result from concatenating assignment operator: "; echo $strvar .= $Intvar + 10; ?>
Output:
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!