Php – String Variable Type

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

[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.

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.

This will output following line.

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.

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  Linux Grep OR, AND, NOT Operator and Logic Examples

Here is some example about them.

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.

The output will be like below.

[rps-include post=6522]

RSS
FACEBOOK
FACEBOOK
PINTEREST
PINTEREST
LINKEDIN
INSTAGRAM

Leave a Reply

Your email address will not be published. Required fields are marked *

Enjoy this blog? Please spread the word :)