Php – String Variable Type – POFTUT

Php – String Variable Type

[rps-include post=6522]

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

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

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";

echo $sentence;

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 

Escape Sequences

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.

  • \n is used for new line
  • \r is used for carriage return
  • \t is used for tab
  • \$ is used for dollar sign
  • \" is used for double quote in string
  • \\ is used for backslash in string
LEARN MORE  Python Variables and Types

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.

String Concatenation

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 $name and $surname by delimiting them with a single space which concatenated between them.



$ fullname = $name." ".$surname;

echo $fullname;

The output will be like below.

İsmail Baydan

[rps-include post=6522]

Leave a Comment