PHP | Strings

PHP | strings are a finite sequence of characters that consists of numbers, letters, special characters, and arithmetic values.

PHP Strings

It is a sequence of characters containing numbers, letters, special characters, etc. It can be specified by a single quote, double-quote, or doc syntax.

Declaring Strings

There are three ways to specify PHP strings.These are:

  • 1.0 single quoted: like 'Learn PHP'
  • 2.0 double quoted: like "Learn PHP OOP"
  • 3.0 heredoc syntax: using heredoc syntax ("<<<")

PHP String - Single quoted

PHP string can be created by enclosing text in a single quote character '.

Source Code

          <?php
$website='sudhakarinfotech is world one of the most popular learning platform for web development.';  
echo $website;  
?>
        
Try it now

To insert a single quote inside the single quote string, just escape that character with a backslash. '

Example

    
     $string = 'Learn sudhakarinfotech's PHP tip and tricks';
   

Source Code

          $stringText = 'Learn sudhakarinfotech's PHP new tip and tricks';
        
Try it now

If you like to put ' inside the single-quoted string then Use to escape the character.

Source Code

          $str = 'Learn web development tricks:';
        
Try it now

Placing Variable Inside Single Quote

Please keep in mind that the variable inside the single quote is not parsed by the PHP parser. Let us understand it with the help of an example.

Source Code

          <?php  
$learningResources ="sudhakarinfotech";
$learnFrontEnd = "Javascript";
$learnWebsite = "Learn web development: $learningResources";
$learnFrontEndDevelopment = 'Learning $learnFrontEnd is really easy.';
echo $learnWebsite."<br>";
//output Learn web development:
echo $learnFrontEndDevelopment;
//Learning $learnFrontEnd is really easy.

?>
        
Try it now

Web Tutorials

PHP Strings
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4