PHP String

String in Php

PHP String is a sequence of characters i.e., used to store and manipulate text. PHP supports only 256-character set and so that it does not offer native Unicode support. There are 4 ways to specify a string literal in PHP.

  1. single quoted
  2. double quoted
  3. heredoc syntax
  4. newdoc syntax (since PHP 5.3)

Single Quoted

We can create a string in PHP by enclosing the text in a single-quote. It is the easiest way to specify string in PHP.

For specifying a literal single quote, escape it with a backslash (\) and to specify a literal backslash (\) use double backslash (\\). All the other instances with backslash such as \r or \n, will be output same as they specified instead of having any special meaning.

Example

<?php

       $str='Hello text within single quote';

       echo $str;

?>

Double Quoted

In PHP, we can specify string through enclosing text within double quote also. But escape sequences and variables will be interpreted using double quote PHP strings.

Example

<?php

$str="Hello text within double quote";

echo $str;

?>

Heredoc

Heredoc syntax (<<<) is the third way to delimit strings. In Heredoc syntax, an identifier is provided after this heredoc <<< operator, and immediately a new line is started to write any text. To close the quotation, the string follows itself and then again that same identifier is provided. That closing identifier must begin from the new line without any whitespace or tab.


Naming Rules

The identifier should follow the naming rule that it must contain only alphanumeric characters and underscores, and must start with an underscore or a non-digit character.

Valid Example

<?php

    $str = <<<Demo

It is a valid example

Demo;    //Valid code as whitespace or tab is not valid before closing identifier

echo $str;

?>

Invalid Example

<?php

    $str = <<<Demo

It is Invalid example

       Demo;    //Invalid code as whitespace or tab is not valid before closing identifier

echo $str;

?>

Heredoc is similar to the double-quoted string, without the double quote, means that quote in a heredoc are not required. It can also print the variable’s value.

Example

<?php

    $city = 'Delhi';

    $str = <<<DEMO

Hello! My name is ......, and I live in $city.

DEMO;

    echo $str;

 ?>

Newdoc

Newdoc is similar to the heredoc, but in newdoc parsing is not done. It is also identified with three less than symbols <<< followed by an identifier. But here identifier is enclosed in single-quote, e.g. <<<‘EXP’. Newdoc follows the same rule as heredocs.

The difference between newdoc and heredoc is that – Newdoc is a single-quoted string whereas heredoc is a double-quoted string.

Example

<?php

    $str = <<<'DEMO'

    Welcome to AskAtul

           Learn with newdoc example.

DEMO;

echo $str;

echo '</br>';



echo <<< 'Demo'    // Here we are not storing string content in variable str.

    Welcome to AskAtul.

           Learn with newdoc example.

Demo;

?>

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *