Exforsys

Home arrow Technical Training arrow PHP Tutorial

Table of Contents

 PHP Strings
 String Functions

PHP Strings

Page 1 of 2
Author: Harsha M V     Published on: 18th Mar 2009

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)

Ads

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

Sample Code
  1. <?php
  2. echo (&lsquoI&rsquom a PHP programmer&rsquo)
  3. echo ('<br>')
  4. echo (&lsquoThis is the way to output a back slash  and forward slash /')
  5. ?>
Copyright exforsys.com


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.

Sample Code
  1. <?php
  2. echo ""I&rsquom a PHP programmer""
  3. echo "This is the way to output a back slash  and forward slash /&rdquo
  4. ?>
Copyright exforsys.com


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:

Sample Code
  1. <?php
  2. $variable = <<<EOT
  3. String CONTENT
  4. EOT
  5.  
  6. echo <<<EOT
  7. My name is "$name". I am printing some $var->object.
  8. Now, I am printing some {$var->object[1]}.
  9. This should print a capital 'A': x41
  10. EOT
  11. ?>
Copyright exforsys.com


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.

Ads

Syntax:

Sample Code
  1. <?php
  2. $variable = <<<&rsquoEOT&rsquo
  3. String CONTENT
  4. EOT
  5.  
  6. echo <<<&rsquoEOT&rsquo
  7. My name is "$name". I am printing some $var->object.
  8. Now, I am printing some {$var->object[1]}.
  9. This should print a capital 'A': x41
  10. EOT
  11. ?>
Copyright exforsys.com




 
This tutorial is part of a PHP Tutorial tutorial series. Read it from the beginning and learn yourself.

PHP Tutorial

 

Comments