Home » Programming » php » PHP String

PHP String

String means set of characters or letters or combination in single or double quotes. Like ‘PHP Language’. If you placed the number in the single quote or double quotes it’s also a string. Like ‘12345’. Similarly, special characters symbol whatever if you write it in the single or double quote it’s become a string.
In PHP a single letter or character also a string. Like ‘A’

Creating and accessing String

In PHP language whatever you write in a code with a single or double quotation, it creates the string.
To declare a string always use the quotation, if you forget to mention quotes give you an error.

Syntax of String

Note: gettype() is a predefine PHP function which used to know the datatype of any variable. In this above example gettype() function return string.

Example

Output

PHP is a very good language and easy to learn And it supports many other languages like HTML, CSS, JAVASCRIPT and so on.

Explanation
You can write a string with single quotes or double quotes both are equal. In above example $var and $var2 both hold string value.

Another Syntax of String:

Explanation

In the first example, we want to print I’d buy myself, in this case, we never declare this string with the single quote because in the original value there has a single quote already. Means in PHP language you never used the same type of quotes so start with the double quote.
In the second example, it returns Sum of 2 & 4 is $sum because we declare the string with single quote and variable also treated as a string in the single quote so it’s can’t return the value of $sum. So never used the variable in a single quote
But in the third example, we receive the value of $sum because double quote treats a variable like a variable so we can receive the value of any variable, not the variable name.
And in the fourth example, we used two type of quotation to declare a string. Because above example, we need to use nested quotes under the main quotes. And in PHP language does not support the same type of quotes as a nested quote.
So in this type of case start with the single quote and used the double quote as a nested quote or start with double quotes and used the single quote as a nested quote.
If you don’t maintain these things PHP gives you an error.

Avoiding the single quote double quote complexity you also used escape sequences (\). PHP used \ symbols as an escape sequence.
Using this escape sequence you also rewrite those strings like this,

Syntax of String with \:

Another way to Create String (HereDoc):
In complicated cases, you need to create the large string. And there you have to follow all rules of quotation or escape sequence to avoid error.
In PHP here has a new way to write the complex string with the simple manner.
3.1 Syntax of HereDoc:

Output

This is another way of showing String Delimiters The value is Hello World
This is another way of showing String Delimiters The value is Hello World

Explanation
<<< is a heredoc symbol. After <<< symbol an identifier is provided, then a new line. Then you write your string and complete the string the same identifier again to close. The closing identifier must be same as beginning in the first line and the identifier must follow the same case. Identifier only starts with character or string or underscore (_) means it must contain only alphanumeric characters. In above example, EOF and tutorial are the identifiers.

Please follow and like us:
error