Php – String Variable Type
Php have variable type
string to store text,char or string values. String type generally used to store values like name, surname, explanations, address, questions. String variables do not have any size or length limit theoretically. The only limit is the system memory limit. String can be defined in two ways like below.
- Single Quote
- Double Quote
Single quote string definition is very the same of double quote string definition. In this example we will define string variable named
$city in a single quote.
Double quote is similar to the single quote but there is one crucial difference. Other variables or expressions can be using in double quote string. Now first look howto use double quote to define a string.
$city = "Ankara";
Now as we stated previously use variable named
$city in new double quote string definition. New string variable will be
$sentence and will include $city variable value inside it.
$sentence="I live in $city";
This will output following line.
I live in Ankara
Multi Line String
String generally defined as single line an short length. But in some situations we may need to define multi line strings. This is very easy where we will just use multi line in single or double quote like below. Both single and double quote can be used to define multi line string.
echo 'Hello World
While interacting with string we may need some control or escape secuence characters. These characters are used to insert some special functions. Here the list of escape sequence characters.
\nis used for new line
\ris used for carriage return
\tis used for tab
\$is used for dollar sign
\"is used for double quote in string
\\is used for backslash in string
Here is some example about them.
echo "This is new line \n";
echo "This is carriage return\r\n";
echo "This is \t a tab\n";
echo "This is \$ sign\n";
echo "This is \"double quote\"\n";
echo "This is \\ backslash\n";
And this code will output like below.
The last topic about string is string concatenation. Actually we will look string related operations in detail next chapter but concatenation is important and useful operation. Concatenation can be done with point
. operator like below.
In this example we concatenate two string variables named
$surname by delimiting them with a single space which concatenated between them.
$ fullname = $name." ".$surname;
The output will be like below.