PHP BASIC : Simple way to specify string in php

By: Array99      Posted On: 01 Dec, 2018      Category: PHP

In this blog, it will be described basic thing about specifying a string in PHP. Strings are always an integral part of the programming. Strings can do something that almost impossible to do with other data types. Sometimes, these strings are simple and used as is and also many times, these strings need to be dynamic. It needs to adapt to what’s going on inside our code.

Generally, we specify a string by two ways in php. But not, PHP string can be specified in four ways.

1. Single Quoted String : 

This is the first way to specify a string. In this way, we enclose a string using single quotes(‘). Single Quoted Strings display things completely as is.  Let's take an example:

<?php
    $var = 'good';
    echo 'Array99 is a $var blog';
    //Output : Array99 is a $var blog
?>

In above example, it will not display value of variable $var because string is being specified using single quote and treated as a plain string. When string is specified in single quote, PHP doesn't evaluate it or interpret escape characters except single quote with backslash (\‘) and backslash which has to be escaped. Ex:

<?PHP
    echo 'Array99 is a \'good\' blog.';
    //Output : Array99 is a 'good' blog.
?>

2. Double Quoted String :

In this way, string is enclosed using double quotes(“). When we use double quotes to enclose a string, PHP interprets escape sequences for special characters like \n, \r, \t etc. And also evaluates variables in the string. For example:

<?php
    $var = "good";
    echo "Array99 is a $var blog.";
    //Output : Array99 is a good blog.
?>

In the above example, value of  $var will display because it is evaluated as variable by PHP within double quotes. 

There is a important point here, you can also use curly braces to isolate the names of variable that you want to evaluate as variables. Let's take an example:

<?php
    $var1 = "good";
    $var2 = "helpful";
    echo "Array99 is a {$var1} and {$var2} blog.";
    //Output : Array99 is a good and helpful blog.
?>

3. Heredoc syntax : 

The third way to delimit strings in PHP is the heredoc syntax: <<<. After this operator, an identifier is provided and then a newline. Now, put string value then the same identifier will be used again to close the quotation. Ex:
 

<?php
    $var = <<<EOT
    Array99 is good blog and it is very helpful.
EOT;
?>

It is very important that the closing identifier must contain no any other characters, except a semicolon (;). This means that identifier may not be indented.

<?php
$var = <<<EOT
Array99 is good blog and it is very helpful.
        EOT;
// It will generate a syntax error. Because there are spaces with closing identifier
?>

Heredoc syntax behaves like double quoted strings without the double quotes. This means that quotes in a heredoc do not need to be escaped. And also variables are evaluated similar to double quoted string. Ex:

<?php
    $var = "good";
    $str = <<<EOT
        Array99 is a $var blog.
EOT;
echo $str;
//Output : Array99 is a good blog.
?>

4. Nowdoc syntax : 

Nowdoc syntax is similar to Heredoc syntax, but no parsing is done inside a nowdoc. Nowdoc is similar to single quoted strings. A nowdoc is identified with the same <<< sequence used for heredoc but the identifier is enclosed in single quotes and all rules for the heredoc identifier also apply to nowdoc identifier. Ex:

<?php
    $var = <<<'EOT'
    Array99 is good blog and it is very helpful.
EOT;
    echo $var;
    //Output : Array99 is good blog and it is very helpful.
?>

 

You may like: