Exforsys.com
 
Home Tutorials PHP
 

PHP Strings

 
Author: Harsha M V
Category: PHP
Comments (0)

Table of Contents

 PHP Strings
 String Functions

PHP Strings

Page 1 of 2

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)

 

Sponsored Links

 

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.

 

Sponsored Links

 

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


Next Page: String Functions


Read Next: How to Use Cookies in PHP



 

 

Comments



Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

 
 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape