Logo

Navigation
  • Home
  • Services
    • ERP Solutions
    • Implementation Solutions
    • Support and Maintenance Solutions
    • Custom Solutions
    • Upgrade Solutions
    • Training and Mentoring
    • Web Solutions
    • Production Support
    • Architecture Designing
    • Independent Validation and Testing Services
    • Infrastructure Management
  • Expertise
    • Microsoft Development Expertise
    • Mobile Development
    • SQL Server Database and BI
    • SAP BI, SAP Hana, SAP BO
    • Oracle and BI
    • Oracle RAC
  • Technical Training
    • Learn Data Management
      • Business Intelligence
      • Data Mining
      • Data Modeling
      • Data Warehousing
      • Disaster Recovery
    • Learn Concepts
      • Application Development
      • Client Server
      • Cloud Computing Tutorials
      • Cluster Computing
      • CRM Tutorial
      • EDI Tutorials
      • ERP Tutorials
      • NLP
      • OOPS
      • Concepts
      • SOA Tutorial
      • Supply Chain
      • Technology Trends
      • UML
      • Virtualization
      • Web 2.0
    • Learn Java
      • JavaScript Tutorial
      • JSP Tutorials
      • J2EE
    • Learn Microsoft
      • MSAS
      • ASP.NET
      • ASP.NET 2.0
      • C Sharp
      • MS Project Training
      • Silverlight
      • SQL Server 2005
      • VB.NET 2005
    • Learn Networking
      • Networking
      • Wireless
    • Learn Oracle
      • Oracle 10g
      • PL/SQL
      • Oracle 11g Tutorials
      • Oracle 9i
      • Oracle Apps
    • Learn Programming
      • Ajax Tutorial
      • C Language
      • C++ Tutorials
      • CSS Tutorial
      • CSS3 Tutorial
      • JavaScript Tutorial
      • jQuery Tutorial
      • MainFrame
      • PHP Tutorial
      • VBScript Tutorial
      • XML Tutorial
    • Learn Software Testing
      • Software Testing Types
      • SQA
      • Testing
  • Career Training
    • Career Improvement
      • Career Articles
      • Certification Articles
      • Conflict Management
      • Core Skills
      • Decision Making
      • Entrepreneurship
      • Goal Setting
      • Life Skills
      • Performance Development
      • Personal Excellence
      • Personality Development
      • Problem Solving
      • Relationship Management
      • Self Confidence
      • Self Supervision
      • Social Networking
      • Strategic Planning
      • Time Management
    • Education Help
      • Career Tracks
      • Essay Writing
      • Internship Tips
      • Online Education
      • Scholarships
      • Student Loans
    • Managerial Skills
      • Business Communication
      • Business Networking
      • Facilitator Skills
      • Managing Change
      • Marketing Management
      • Meeting Management
      • Process Management
      • Project Management
      • Project Management Life Cycle
      • Project Management Process
      • Project Risk Management
      • Relationship Management
      • Task Management
      • Team Building
      • Virtual Team Management
    • Essential Life Skills
      • Anger Management
      • Anxiety Management
      • Attitude Development
      • Coaching and Mentoring
      • Emotional Intelligence
      • Stress Management
      • Positive Thinking
    • Communication Skills
      • Conversation Skills
      • Cross Culture Competence
      • English Vocabulary
      • Listening Skills
      • Public Speaking Skills
      • Questioning Skills
    • Soft Skills
      • Assertive Skills
      • Influence Skills
      • Leadership Skills
      • Memory Skills
      • People Skills
      • Presentation Skills
    • Finding a Job
      • Etiquette Tips
      • Group Discussions
      • HR Interviews
      • Interview Notes
      • Job Search Tips
      • Resume Tips
      • Sample Resumes
 

PHP Strings

By Harsha M V | on March 18, 2009 |
PHP Tutorial

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

  1. <?php
  2. echo (‘I’m a PHP programmer’);
  3. echo ('<br>');
  4. echo (‘This is the way to output a back slash  and forward slash /');
  5. ?>

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.

  1. <?php
  2. echo ""I’m a PHP programmer"";
  3. echo "This is the way to output a back slash  and forward slash /”
  4. ?>

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:

  1. &lt;?php
  2. $variable = &lt;&lt;&lt;EOT
  3. String CONTENT
  4. EOT;
  5.  
  6. echo &lt;&lt;&lt;EOT
  7. My name is &quot;$name&quot;. I am printing some $var-&gt;object.
  8. Now, I am printing some {$var-&gt;object[1]}.
  9. This should print a capital 'A': x41
  10. EOT; 
  11. ?&gt;

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:

  1. &lt;?php
  2. $variable = &lt;&lt;&lt;&rsquo;EOT&rsquo;
  3. String CONTENT
  4. EOT;
  5.  
  6. echo &lt;&lt;&lt;&rsquo;EOT&rsquo;<br />
  7. My name is &quot;$name&quot;. I am printing some $var-&gt;object.
  8. Now, I am printing some {$var-&gt;object[1]}.
  9. This should print a capital 'A': x41
  10. EOT; 
  11. ?&gt;

String Functions:

print()

print function outputs a string

Example:

int print (string $arg)

print() is not actually a real function (it is a language construct) so you are not required to use parentheses with its argument list.

Various Method of Using print:

  1. &lt;?php
  2. print(&quot;Hello World&quot;);
  3. print &quot;print() also works without parentheses.&quot;;
  4. print &quot;This spans
  5. multiple lines. The newlines will be
  6. output as well&quot;;
  7. print &quot;This spans multiple lines. The newlines will be n output as well.&quot;;
  8. print &quot;escaping characters is done &quot;Like this&quot;.&quot;;
  9.  
  10. print &lt;&lt;&lt;END
  11. This uses the &quot;here document&quot; syntax to output
  12. multiple lines with $variable interpolation. Note
  13. that the here document terminator must appear on a
  14. line with just a semicolon no extra whitespace!
  15. END; //(USING HEREDOC)
  16.  
  17. print $foo; //(Printing Variables)
  18. ?&gt;

echo

echo — Output one or more strings

Example:

void echo ( string $arg1 $arg2 );

Outputs all parameters.

echo() is not actually a function (it is a language construct), so you are not required to use parentheses with it. echo() (unlike some other language constructs) does not behave like a function, so it cannot always be used in the context of a function. Additionally, if you want to pass more than one parameter to echo(), the parameters must not be enclosed within parentheses.

Various Method of Using echo:

  1. &lt;?php
  2. echo &quot;Hello World&quot;;
  3. echo &quot;This spans
  4. multiple lines. The newlines will be
  5. output as well&quot;;
  6. echo &quot;This spans multiple lines. The newlines will be n output as well.&quot;;
  7. echo &quot;Escaping characters is done &quot;Like this&quot;.&quot;;
  8. echo $foo;
  9. echo $foo,$bar; 
  10. echo &lt;&lt;&lt;END
  11. This uses the &quot;here document&quot; syntax to output
  12. multiple lines with $variable interpolation. Note
  13. that the here document terminator must appear on a
  14. line with just a semicolon. no extra whitespace!
  15. END;
  16. echo $some_var ? 'true': 'false';
  17. ?&gt;

sprintf

sprintf returns a string produced according to the formatting string format.  Type Specifier that says what type the argument data should be treated as. The conversion specifiers are useful to format or transform the values. Each conversion specifier starts with a single percent symbol%) and ends with a conversion character (one ofb, c,d,f,o,s, u,x, orX ). Please refer http://in2.php.net/manual/en/function.sprintf.php for complete list of specifiers along with descriptions.

Example

 

  1. &lt;?php 
  2. $str = &quot;Hello&quot;; 
  3. $number = 123; 
  4. $txt = sprintf(&quot;%s world. Day number %u&quot;,$str,$number); 
  5. echo $txt; 
  6. ?&gt;

Output

Hello world. Day number 123

print_r:

Prints human-readable information about a variable

print_r (mixed $expression [, bool $return])

print_r() displays information about a variable in a way that’s readable by humans. print_r(), var_dump() and var_export() will also show protected and private properties of objects with PHP 5. Static class members will not be shown.  Remember that print_r() will move the array pointer to the end. Use reset() to bring it back to beginning.  Main practical use of print_r() function is to output array values.

Example:

  1. &lt;?php
  2. $a = array ('a' =&gt; 'apple', 'b' =&gt; 'banana', 'c' =&gt; array ('x', 'y', 'z'));
  3. print_r ($a);
  4. ?&gt;

Output:

Array
(
    [a] => apple
    [b] => banana
    [c] => Array
    (
        [0] => x
        [1] => y
        [2] => z
    )
)

var_dump:

Var_dump is used to get information about a variable.   This function displays structured information about one or more expressions that includes its type and value. Arrays and objects are explored recursively with values indented to show structure. In PHP 5 all public, private and protected properties of objects will be returned in the output.

Example:

  1. &lt;?php
  2. $a = array(1, 2, array(&quot;a&quot;, &quot;b&quot;, &quot;c&quot;));
  3. var_dump($a);
  4. ?&gt;

Output:

array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
        array(3) {
            [0]=>
            string(1) "a"
            [1]=>
            string(1) "b"
            [2]=>
            string(1) "c"
        }
}

The echo and print functions are similar is all aspects. The only difference between them is that when items are separated using a comma, the echo function performs faster.

« « How to Handle Tough Situations
How to Overcome Resistance » »

Author Description

Avatar

Free Training

RSSSubscribe 394 Followers
  • Popular
  • Recent
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment
  • PHP Tutorials – Loops

    September 30, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHP Tutorials – Functions (Part I)

    September 29, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Functions (Part-2)

    October 7, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Strings (Part I)

    October 13, 2006 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • How to Use Cookies in PHP

    February 18, 2009 - 0 Comment
  • PHP and MySQL User Registration

    October 31, 2008 - 0 Comment
  • WAMP Server

    October 26, 2008 - 0 Comment
  • PHP Tutorial : PHP & MySQL

    January 16, 2007 - 0 Comment
  • PHP Tutorials – Forms

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part-2)

    November 22, 2006 - 0 Comment
  • PHP Tutorials – File manipulation (Part 1)

    November 15, 2006 - 0 Comment
  • PHP Tutorials – Arrays (Part 2)

    November 15, 2006 - 0 Comment
  • PHP Tutorials : Arrays

    November 5, 2006 - 0 Comment
  • PHp Tutorials : Regular expressions

    November 2, 2006 - 0 Comment

Exforsys e-Newsletter

ebook
 

Related Articles

  • How to Use Cookies in PHP
  • PHP and MySQL User Registration
  • WAMP Server
  • PHP Tutorial : PHP & MySQL
  • PHP Tutorials – Forms

Latest Articles

  • Project Management Techniques
  • Product Development Best Practices
  • Importance of Quality Data Management
  • How to Maximize Quality Assurance
  • Utilizing Effective Quality Assurance Strategies
  • Sitemap
  • Privacy Policy
  • DMCA
  • Trademark Information
  • Contact Us
© 2023. All Rights Reserved.IT Training and Consulting
This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish.AcceptReject Read More
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT