Types of String in PHP
String in PHP
In PHP, string is a sequence of characters used to represent text, such as words and sentences. Strings are enclosed in either single quotes (‘ ‘) or double quotes (” “).
Types of String
There are four different ways to define a string literal in PHP:
- Single quote
- Double quote
- Heredoc string
- Nowdoc string
1. Single Quote String: In PHP, Single quotes are used to define simple strings in PHP. The text within single quote means special characters and variables are not interpreted.
Example:
<?php $x = 'Welcome to Webeduclick'; echo $x; ?>
Output:
Welcome to Webeduclick
2. Double Quote String: In PHP, double-quote strings are capable of processing special characters. It returns the string like it was written, with the variable name.
Example:
<?php $x = "World"; echo "Hello $x"; ?>
Output:
Hello World
3. Heredoc String: It is a method for declaring a multi-line string that preserves line breaks and other whitespace. It behaves like a double-quoted string by expanding variables and escape sequences without needing to escape internal quotes.
4. Nowdoc String: It is a syntax for declaring a multi-line, long string literal that behaves exactly like a single-quoted string: variables and escape sequences are not parsed or expanded.