PHP Tutorial - PHP String






A string variable is created using quotation.

In PHP we have two syntax to define strings

$myString = 'string value';

or

$myString = "string value";

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

The following code creates a string in PHP.

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

The code above generates the following result.

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 java2s.com";  
  print($myString);
?>

The code above generates the following result.





Multline strings

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

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

echo $myString;
?>

The code above generates the following result.





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

Here are some examples to make these differences clear:

<?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!"   
?>

The code above generates the following result.

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
\' Print the next character as a single quote rather than treating it as a string terminator
\n Print a new line character
\t Print a tab character
\r Print a carriage return (used primarily on Windows)
\$ Print the next character as a dollar rather than treating it as part of a variable name
\\ Print the next character as a backslash rather than treating it as an escape character

Here is a code example of these escape sequences in action:

<?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;
?>

The code above generates the following result.

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; 
?>

The code above generates the following result.

PHP Variable substitution in String

In PHP we can write variable name into a long string and PHP knows how to replace the variable with its value.

Here is a script showing assigning and outputting data.

<?php
       $name = "java2s.com";
       print "Your name is $name\n";
       $name2 = $name;
       print 'Goodbye, $name2!\n';
?>

The code above generates the following result.

Example 2

Consider the following situation:

<?php
$myValue = "cat"; 
echo "My favorite animals are $myValues";   
?>

The code above generates the following result.

We confused PHP if $myValues is a variable name or it is a plural form of a variable.

To get around this problem using curly brackets, as follows:

<?php
$myValue = "cat"; 
echo "My favorite animals are {$myValue}s";   
?>

The code above generates the following result.

You can also place the opening curly bracket after the $ symbol, which has the same effect:

<?php
$myValue = "cat"; 
echo "My favorite animals are ${myValue}s";   
?>

The code above generates the following result.

We can use the curly brackets to distinguish the variable name from the rest of the string.

Curly bracket syntax can insert more complex variable values, such as array element values and object properties.

Just make sure the whole expression is surrounded by curly brackets:

<?php
$myArray["age"] = 34; 
echo "My age is {$myArray["age"]}"; // Displays "My age is 34"   
?>

The code above generates the following result.

or we can create the string by concatenating the values together:

<?php
$myArray["age"] = 34; 
echo "My age is " . $myArray["age"]; // Displays "My age is 34"    
?>

The code above generates the following result.

PHP String Element

We can accessing characters within a PHP string with [] operator.

We can access the individual characters of a string. To access a character at a particular position, or index , within a string, use:

$stringVariable= "a string value";
$character = $stringVariable[index];   

We place the index between square brackets after the string variable name. String indices start from 0, so the first character in the string has an index of 0, the second has an index of 1, and so on.

You can use [x] notation with strings to read or write individual characters.

<?PHP
$mystr = "java2s.com"; 
$mystr[0] = "H"; 
$mystr[12] = "!"; 
print $mystr; 
?>

The code above generates the following result.

The first character in a string is numbered 0. A string of length 13 has its last character at position 12.

Example 3

The following code shows how to embed variable in hereDoc and string.

<?php
  $name = "Jack";

  //Greet Zeev
  print("Hello, $name!\n");

  //Greet Zeev again
  print <<< EOD
Hello again, $name!
How is it going?
EOD;
?>


The code above generates the following result.