Technical Training
PHP TutorialPHP Strings
This tutorial shows how to handle strings in PHP, different output methods, functions and the significance of using each of them. A string can be literally expressed in the following ways in PHP
1. Single Quoted
2. Double Quoted
3. HEREDOC
4. NOWDOC (from ver. PHP 5.3.0)
Different methods used to handle strings are:
1. Print
2. Echo
3. Sprintf
4. Print_r
5. Var_dump
Single Quoted String:
The simplest way to specify a string is to enclose it in single quotes (the character '). To specify a single quote in your string you will need to escape it with a back slash (). In single quotes all special Characters are not recognised unless they are escaped with the back slash ().
Examples
- <?php
- echo (&lsquo
I&rsquo m a PHP programmer&rsquo ) - echo ('<br>')
- echo (&lsquo
This is the way to output a back slash and forward slash /') - ?>
Single Quotes treats Variables as its face value and does not recognise the special characters unless escaped.
Double Quoted String:
Double quoted string is one of the most common method used to specify a string. It has its own advantages and disadvantages. Double quoted string treat variables by expanding it with its value and it also recognizes all the special characters.
- <?php
- echo ""I&rsquo
m a PHP programmer"" - echo "This is the way to output a back slash and forward slash /&rdquo
- ?>
HEREDOC:
Third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Syntax:
- <?php
- $variable = <<<EOT
- String CONTENT
- EOT
- echo <<<EOT
- My name is "$name". I am printing some $var->object.
- Now, I am printing some {$var->object[1]}.
- This should print a capital 'A': x41
- EOT
- ?>
HEREDOC method can be used whenever you have a lot of HTML to be displayed. It can recognise variables and special characters like the double quoted string.
In the above example EOT can be replaced with any value unless you follow the naming rules. Examples: HMV, ROCK, WEBY
NOWDOC:
Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML <![CDATA[ ]]> construct, in that it declares a block of text which is not for parsing.
A nowdoc is identified with the same <<< seqeuence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.
Syntax:
- <?php
- $variable = <<<&rsquo
EOT&rsquo - String CONTENT
- EOT
- echo <<<&rsquo
EOT&rsquo - My name is "$name". I am printing some $var->object.
- Now, I am printing some {$var->object[1]}.
- This should print a capital 'A': x41
- EOT
- ?>
PHP Tutorial
- WAMP Server
- PHP and MySQL User Registration
- PHP Strings
- How to Use Cookies in PHP
- PHP Tutorial : PHP & MySQL
- PHP Tutorials – Forms
- PHP Tutorials - File manipulation (Part-2)
- PHP Tutorials – File manipulation (Part 1)
- PHP Tutorials - Arrays (Part 2)
- PHP Tutorials : Arrays
- PHp Tutorials : Regular expressions
- PHP Tutorials : Strings (Part 2)
- PHP Tutorials - Strings (Part I)
- PHP Tutorials – Functions (Part-2)
- PHP Tutorials - Functions (Part I)
- PHP Tutorials - Loops
- PHP Tutorials - Conditional Statements
- PHP Tutorials - Operators
- PHP Tutorials - Operators (Part I)
- PHP Tutorials - Variables
- PHP Tutorials - Data Types
- PHP Tutorial - Syntax
- PHP Tutorial - Installation
- PHP Tutorial - Introduction







