|
|
Tutorials » Php »
|
Topic |
How to define a string variable and assign value?
|
|
Explanation |
A string is a sequence of characters. A string can be delimited by single quotes or double quotes.
The declaration of a string variable is same as that of other variable. The only difference
is that the value that you want to store in a string variable must be enclosed in inverted commas or quotes.
For Example:
$str1 = "This is a string datatype variable";
$str2 = 'This is also a string datatype variable';
String declared within double-quotes are subject to variable substitution and escape sequence handling,
while string declared within single-quotes will not perform variable subsitution and escape sequence handling.
For example:
$str="Integer";
echo "String\t$str\n";
RESULT:
String Integer
This displays "String" followed by a space and then "Integer" followed by a newline.
Here variable substitution is performed on the variable $str and the escape sequences are converted to their corresponding characters.
echo 'String\t$str\n';
RESULT:
String\t$str\n
In this case, the output is exactly "String\t$str\n". There is no variable substitution or handling of escape sequences,
the string within the inverted commas are printed as such.
String Concatenation is a process of adding two strings. This is done by attaching one string to the end of another string.
This is done by using '.' (period) operator as shown below
$str1="Hello";
$str2="World";
echo "$str1"."$str2";
RESULT:
HelloWorld
This displays value of $str1 as "Hello" followed by value of $str2 as "World".
|
|
|
|