PHP String

Basically PHP string is a non-numeric data type. It is sequence of characters and it can store letters, numbers, and even special characters.

Creating PHP String

There are four ways of creating php string:

  • Single-quote strings
  • Double-quote strings 
  • Heredoc
  • Newdoc

Most common ways of creating strings are using single/ double quotation, So will discuss them in detail.

Single-quote strings:

In PHP , Single-quote string is created by enclosing the text in a single quotation mark. Special characters within quotes are not processed by Single-quote string.

Lets see an example below:

<?php
 
// single-quote strings
 
$name  = 'Tutorials art';
 
echo 'Welcome to $name';
echo "</br>";
echo 'Welcome to Tutorials art ';
?>
PHP str

The echo expression in the preceding example prints the variable name rather than the contents of the variables. Since PHP does not process special characters in single-quote strings. As a result, the string is unable to recognize the ‘$’ sign as the beginning of a variable name.

Double-quote strings:

In PHP , double-quote string is created by enclosing the text in a double quotation mark. Special characters within quotes can be processed by double-quote string, So double-quote String can interpret escape sequences and variables.

<?php
 
// single-quote strings
 
$name  = 'Tutorials art';
 
echo "Welcome to $name";

?>

In the above example the double-quote strings is processing the special characters according to their properties. 

Heredoc:

As compared to double quotes, the heredoc technique is used to generate fairly complex strings. Heredoc allows programmers to construct multi-line strings without using quotation marks, but it do not allow php string concatenation.

Note: While using heredoc there is no need of escape characters to double quote a word within a string.

Syntax:

You’ll need to open a string with <<< and than an identifier(any name/variable), such as text. After that, you can write any text, than to close the heredoc, just write the identifier you wrote in starting and than end it with semicolon.

Lets discuss an example below to understand it more clearly.

<?php

$name = "HTML";

echo <<<lang

     To learn $name,

     You can visit "Tutorials art".

lang;

?>

Note: The closing identifier after string must begin from new line and also make sure there should no whitespace before and after identifier, else it will give error.

Lets see example :

<?php

$name = "HTML";

echo <<<lang

     To learn $name, 

     You can visit "Tutorials art".

          lang;

?>
Error heredoc

Newdoc

Newdoc is similar to heredoc, but it does not perform parsing. The syntax is identical to that of heredoc, with a symbol accompanied by an identifier, but here identifier is enclosed in single quotes.

<?php

$name = "HTML";

echo <<<'lang'

     To learn $name, 

     You can visit "Tutorials art".
lang;

echo "</br> </br>";
 echo <<<'lang'

     To learn HTML, 

     You can visit "Tutorials art".
lang;

?>
PHP string

Note: The closing identifier after string must begin from new line and also make sure there should no whitespace before and after identifier, else it will give error.

The distinction between newdoc and heredoc is that, Newdoc work as single-quoted string while heredoc work as double-quoted string, despite the fact that their syntax is nearly identical.

There are some php functions for string, So you can visit PHP string functions to learn about these functions.