php Strings


A string is a sequence of characters, like "Hello world!". where a character is the same as a byte. This means that PHP only supports a 256-character set, and hence does not offer native Unicode support.


 Note: As of PHP 7.0.0, there are no particular restrictions regarding the length of a string on 
	      64-bit builds. On 32-bit builds and in earlier versions, a string can be as large as up to 
	      2GB (2147483647 bytes maximum) 

In PHP we have two syntax to define strings


$myString = 'string value';


or


$myString = "string value";


Single Quote


The first form uses the single quotation and the second form uses the double quotation.


To specify a literal single quote, escape it with a backslash (\). To specify a literal backslash, double it (\\). All other instances of backslash will be treated as a literal backslash: this means that the other escape sequences you might be used to, such as \r or \n, will be output literally as specified rather than having any special meaning.


The following code creates a string in PHP.


		  		<?PHP
				  $myString = 'hello from studentsempire.com';   
				  print($myString);
				?>
		  

Output


		  		hello from studentsempire.com		  

Double Quote


In this example, the string literal is enclosed in single quotation marks ('). You can also use double quotation marks ("), as follows:


		  		<?PHP
				    $myString = "hello from studentsempire.com";  
  					print($myString);
				?>
		  

Output


		  		hello from studentsempire.com		  

Note: Unlike the double-quoted and heredoc syntaxes, variables and escape sequences for special characters will not be expanded when they occur in single quoted strings.


Multline strings


To specify multline strings in PHP, insert newlines into the string literal between the quotation marks:


		  		<?PHP
				    $myString = " 
					    demo
					    from
					    studentsempire.com
					";   

					echo $myString;
				?>
		  

Output


		  		 
					    demo
					    from
					    studentsempire.com
							  

PHP Single vs double quotation string


Single and double quotation marks work in different ways.


If you enclose a string in single quotation marks, PHP uses the string exactly as typed.


However, double quotation marks has extra features:


  • variable names within the string are parsed and replaced with the variable's value
  • special characters need escaping in the string

		  		<?PHP
				    $myString = 'world'; 
					echo "Hello, $myString! \n"; // Displays "Hello, world!" 
					echo 'Hello, $myString! \n'; // Displays "Hello, $myString!" 
					echo " Hi\tthere! "; // Displays "Hi      there!" 
					echo ' Hi\tthere! '; // Displays "Hi\tthere!" 
				?>
		  

Output


		  		Hello, world! 
Hello, $myString! \n Hi there! Hi\tthere!

Using double quotes causes the $myString variable name to be substituted with the actual value of $myString.


However, when using single quotes, the text $myString is retained in the string as-is.


With the "Hi there!" example, an escaped tab character (\t) is included within the string literal.


When double quotes are used, the \t is replaced with an actual tab character; hence the big gap between Hi and there! in the output. The same string enclosed in single quotes results in the \t characters being passed through intact.


Within single-quoted strings, we can use a couple of escape sequences. Use \' to include a literal single quote within a string.


To include the literal characters \' within a single-quoted string, use \\\'.


PHP String Escape Sequences


Escape sequences, the combination of the escape character \ and a letter, are used to signify the escape character.


The valid escape sequences in PHP are shown in the following table.


Escape Meaning
\" Print the next character as a double quote rather than treating it as a string terminator or double-quote
\' Print the next character as a single quote rather than treating it as a string terminator or single quote
\n Print a new line character or linefeed (LF or 0x0A (10) in ASCII)
\t Print a tab character or horizontal tab (HT or 0x09 (9) in ASCII)
\r Print a carriage return (used primarily on Windows) or carriage return (CR or 0x0D (13) in ASCII)
\$ Print the next character as a dollar rather than treating it as part of a variable name or dollar sign
\\ Print the next character as a backslash rather than treating it as an escape characte or backslash
\v vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5)
\e escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4)
\f form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5)
\[0-7]{1,3} the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000")
\x[0-9A-Fa-f]{1,2} the sequence of characters matching the regular expression is a character in hexadecimal notation
\u{[0-9A-Fa-f]+} the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint's UTF-8 representation (added in PHP 7.0.0)

		  		<?PHP
					$MyString = "This is an \"escaped\" string"; 
					print $MyString;
					$MySingleString = 'This \'will\' work'; 
					print $MySingleString;
					$MyNonVariable = "I have \$abc"; 
					print $MyNonVariable;
					$MyNewline = "This ends with a line return\n"; 
					print $MyNewline;
					$MyFile = "c:\\windows\\system32\\myfile.txt"; 
					print $MyFile;
				?>
		  

output


		  		This is an "escaped" stringThis 'will' workI have $abcThis ends with a line return
c:\windows\system32\myfile.txt		  

Since the only escape sequence that works in single quotes is \', it is safe to use non-escaped Windows-style filenames in your single-quoted strings, like this:


		  		<?PHP
					$filename = 'c:\windows\me.txt'; 
					print $filename; 
				?>
		  

output

		  		c:\windows\me.txt		  




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion