Please support if you like my work by payment through upi: sinhamit@icici or payment by bank
account name: Amit Kumar Sinha,
account number: 2646728782
IFSC code: KKBK0005660
SWIFT: KKBKINBB
String Concatenation in Category: PHP by amit
🕙 Posted on 2023-05-10 at 07:34:40 Read in Hindi ...
More about Strings
You have seen various string functions in previous page. Now, you will learn about concatenation (joining) of one or more string with each other, and with another data-type values. In many programming languages, for example, JavaScript and Python, this operation is done with + (plus key in the keyboard). But in PHP, concatenation of a string with another string or another data-type (the latter will be type-juggled into string data-type) is done with .
(dot) symbol.
Many programmers and beginners, when diverting from other programming languages, or have not enough practing or have been abondoned PHP for sometime, may be confused with this difference! Hope, in later versions of PHP, developers will also incorporate + (plus key) for concatenation.
<?php
echo "Hello " . "World"; // outputs Hello World
echo '<br />';
echo "Hello" . " " . "World"; // outputs Hello World
echo '<br />';
echo "Hello"."World"; // outputs HelloWorld ?>
In the above example, you have seen that "
(a double quote, at the left of Enter/'
(a single quote, at the left of the Enter/
In the sixth line of above example, you have noticed that when blank space is not provided either within the string, or in separate string (a pair of double quotes, in fourth line), the two words (strings) joined without a space between them. After the opening PHP tag, you must have to place a blank space. Thereafter, even after the echo
function, you don't need a blank space, when you are placing any character inside a pair of double quotes or a pair of single quotes. The codes in above example can be written as follows:
<?php
echo "Hello "."World";echo '<br />';echo "Hello"." "."World";echo'<br />';echo "Hello"."World";?>
The <br /> HTML element is important, to provide a line-break between lines. You can provide (UNICODE) to make more space within a string. You will later learn, how to insert escaped characters, for example, \n to insert line-break, but the output will not be properly displayed. This is not because PHP has any error, but it is due to design of the web-browser, which displayed all line-breaks and blank spaces within HTML structure, as they have been entered by the keyboard. You can view the OUTPUT in view-source: by pressing the Ctrl U keys, when the result is displayed in the web-browser.
Concatenation Operator
The .
(dot) and .=
are known as concatenation operator. You have seen the use of .
(dot) in above examples, however, .=
operator is generally used with variables. Consider the following examples:
<?php
$my_var = "Good ";
$greeting1 = "morning";
$greeting2 = "afternoon";
$greeting3 = "evening";
$breakfast = $my_var; $breakfast .= $greeting1;
$lunch = $my_var; $lunch .= $greeting2;
$tea = $my_var; $tea .= $greeting3;
echo $breakfast; // outputs Good morning
echo '<br />'; echo $lunch; // outputs Good afternoon
echo '<br />'; echo $tea; // outputs Good evening?>
In the above example, $my_var, $greeting1, $greeting2, $greeting3, $breakfast, $lunch, and $tea variable names are declared, and first four variables are assigned with the string data-type literals. Last three variable names are first assigned with the value of $my_var, and then each of these three variables are concatenated with $greeting1, $greeting2, or $greeting3 respectively.
Many persons including myself, as beginners, have been confused with meaning of literal. Think a literal as a currency note, for example, in USA and many countries $1 bill (note) has some value. $1 bill (note) or Rs.500 currency note (in India, and many countries), and other currency notes/null
by Government of India, but other currency notes/
If you don't understand how this works, consider following examples, which are equal to (same as) above example. When you are testing either above example, or following codes, you should be careful that any variable name whose value is to be displayed with echo
function or used in any other (pre-defined or user-defined) function, must have some value, either null
or any other data. Remember the REQUEST-RESPONSE CYCLE, you have read in previous page.
In any PHP file, you must have to assign some value to a variable name. PHP doesn't have undefined data-type, like JavaScript. In PHP, this will cause ERROR, though value assigned to the variable name will be null
. Re-declaring a variable name doesn't cause any problem, but you cannot re-declare or re-assign value to a constant. You will learn about global and local variables later, which have global and local scope in a REQUEST-RESPONSE cycle. It will elaborate your understanding of defining variable in advance section.
<?php $my_var = "Good "; $greeting1 = "morning"; $greeting2 = "afternoon"; $greeting3 = "evening"; ?>
<?php $breakfast = $my_var . $greeting1; echo $breakfast; ?><br />
<?php $lunch = $my_var . $greeting2; echo $lunch; ?><br />
<?php $tea = $my_var . $greeting3; echo $tea; ?><br />
<?php $my_var = "Good "; $my_var .= "morning"; echo $my_var; // Good morning ?>
You can assign other data-types, for example, boolean, integer, float, null, etc. However, you can not concatenate (join) an array directly with a string data-type. An item inside an array, which is not an array, can be concatenated with a string.
<?php $my_var = "Balance: "; $my_var .= 123.45; echo $my_var; // Balance: 123.45 ?><br />
<?php $my_var = "Balance: "; $my_var .= true
; echo $my_var; // Balance: 1 ?><br />
Leave a Comment:
Amit Sinha March 2nd, 2023 at 9:30 PM
😃 😄 😁 😆 😅 😂 😉 😊 😇 😍 😘 😚 😋 😜 😝 😶 😏 😒 😌 😔 😪 😷 😵 😎 😲 😳 😨 😰 😥 😢 😭 😱 😖 😣 😞 😓 😩 😫 😤
Ribhu March 3rd, 2023 at 9:30 PM
🐵 🐒 🐶 🐩 🐺 🐱 🐯 🐅 🐆 🐴 🐎 🐮 🐂 🐃 🐄 🐷 🐖 🐗 🐽 🐏 🐑 🐐 🐪 🐫 🐘 🐭 🐁 🐀 🐹 🐰 🐇