<<

PHP String PHP String

In PHP, String is a sequence of characters.

Each character in a string is of size byte. Hence, PHP supports only 256-character set.

You can specify a string using single or double quotes. But each of these have their own meaning in interpreting some of the escaped characters. We will learn in detail.

Single Quoted Strings

To define a string in PHP, you can write the sequence of characters enclosed in single quotes.

Following is an example where we initialized a variable with a string using single quotes.

Single quoted string does not interpret escaped sequences like \n, \r, \t, etc. But there is only one exception that you can escape a single quote. So, any character that comes in a single quoted string is considered as just another character, except for the backslash followed by a single quote.

In the following program, we have demonstrated this behavior.

PHP Program

';

$string2 = 'Hello\n\t World!'; echo $string2; ?>

Output Double Quoted Strings

To define a string in PHP, you can write the sequence of characters enclosed in double quotes.

Following is an example where we initialized a variable with a string using double quotes.

Unlike single quoted strings, double quoted strings interpret escaped sequences like \n, \r, \t, etc.

Following table lists the escaped character by double quotes string, along with their description.

Escape Sequence Meaning

\n New line

\r Carriage return

\t Horizontal tab

\v Vertical tab

\e Escape

\f Form feed

\\ Backslash

\$ Dollar sign

\” Double Quote

In the following program, we have demonstrated how double quoted string escapes these special sequences. PHP Program

';

$string = "Hello\r World!"; echo $string, '
';

$string = "Hello\t World!"; echo $string, '
';

$string = "Hello\v World!"; echo $string, '
';

$string = "Hello\e World!"; echo $string, '
';

$string = "Hello\f World!"; echo $string, '
';

$string = "Hello\\ World!"; echo $string, '
';

$string = "Hello\$ World!"; echo $string, '
';

$string = "Hello\" World!"; echo $string, '
'; ?>

Output

Octal Number Sequence

In a double quoted PHP String, you can specify a number using backslash. The octal number would be converted to a single byte. Anything that is overflow the byte is gone. The sequence to write an octal number is

\[0-7]{1,3}

In the following program, we will write a string with octal numbers.

PHP Program

Output

Hexadecimal Number Sequence

In a double quoted PHP String, you can specify a number using backslash. The hexadecimal number would be converted to a single byte. Anything that is overflow the byte is gone.

The sequence to write a hexadecimal number is

\x[0-9A-Fa-f]{1,2}

In the following program, we will write a string with hexadecimal numbers.

PHP Program

Output

Conclusion

In this PHP Tutorial, we learned what Strings in PHP are, how to define a , different ways to write a string, and their importance. PHP Tutorial

⊩ PHP Tutorial

⊩ PHP Boolean

⊩ PHP Integer

⊩ PHP String

PHP Conditional Statements

⊩ PHP If

⊩ PHP If AND

⊩ PHP If OR

⊩ PHP If NOT

PHP Loop Statements

⊩ PHP While

PHP Strings

⊩ PHP String Length

⊩ PHP String - Count Number of Words

⊩ PHP String Reverse

⊩ PHP String - Loop thorugh Characters

⊩ PHP String - Loop through Words

⊩ PHP Split String into Words

PHP Arrays

⊩ PHP Create Array

⊩ PHP String Array

⊩ PHP Array Notice Undefined Offset

⊩ PHP Array Count

⊩ PHP Array Foreach