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 Tutorials – Strings (Part I)

By Exforsys | on October 13, 2006 |
PHP Tutorial

PHP Tutorials – Strings (Part I)

In this PHP Tutorial you will learn about Strings viz The heredoc syntax, String length, String position, Strings comparison, String search and Substring selection

The heredoc syntax:

PHP has another way to specify strings called heredoc syntax. The heredoc syntax is very useful for specifying large text. The heredoc syntax is very useful for specifying text that contains quotes and double quotes, because they don’t need to be escaped. Heredoc syntax starts with a <<< and a label which is an identifier, and ends with the identifier label,

for example:

    echo <<<NAMEFORM   
        <FORM METHOD=”POST” ACTION=”{$_ENV[‘PHP_SELF’]}”>
            <INPUT TYPE=”TEXT” NAME=”FIRSTNAME” VALUE=”$firstname”>
            <INPUT TYPE=”SUBMIT” NAME=”SUBMIT” VALUE=”SUBMIT”>
        </FORM>
    NAMEFORM;

String length:

To find the string length, use the function strln(). strlen takes one argument, which is the string.

Example:

    <?php
        $s = “This is a string”;
        echo strln($s);
    ?>

This will print 16

String position:

To get a position of a string inside a string, use the function strops(). strpos() takes two arguments, the string to search in, and the string to search for.

Example:

    <?php
        $s = “This is a string”;
        echo strln(‘Location of r is ‘ . strops($s, ‘r’));
    ?>

This will print 12, r is the 13th character of the string, but strings index start with zero.

Strings comparison:

To compare two string for equality, you can use ==.
PHP provide a function for strings comparison, the function is strcmp().
strcmp() takes two arguments, these are the two strings to be compared, the comparison returns one of the following values:

    • A negative number if the first string is less than the second string.
    • A positive number if the second string is less than the first string.
    • Zero if both are identical.

Example:

    <?php
        $s1 = “This is a string”;
        $s2 = “This is a string”;
        if(strcmp($s1, $s2) === 0) {
        echo ‘Strings are equal’;
        } else {
        echo ‘Strings are not equal’;
    }
?>

If you want to neglect the characters case, use the function strcasecmp().

String search:

To search for a string inside another string, use the function strstr().
strstr() takes two arguments, the string to search in, and the string to search for.
strstr() returns on of the following values:

    • If the string is found, it will return the part of the first string that starts with the second string.
    • If the string is not found, it will return false.

Example:

    <?php
        $s1 = “This is a string”;
        $s2 = “is”;
        echo strstr($s1, $s2);
    ?>

This will print ‘is a string’.

If you want to neglect the characters case, use the function strtistr().

Substring selection:

To select a portion of a string, use the function substr().
substr() takes two or three arguments, the string to get a portion of it, an integer that defines the start index of the portion, and the optional third argument is the end index of the portion.
Substr() returns one of the following values:

    • If two arguments are used, it will return the portion of the string starting from the given index to the end of the string.
    • If three arguments are used, it will return the portion of the string starting from the index specified by the second arguments, and with the length specified by the third argument.

Example:

    <?php
        $s1 = “This is a string”;
        echo substr($s1, 5);
    ?>

This will print ‘is a string’

    <?php
        $s1 = “This is a string”;
        $s2 = “is”;
        echo strstr($s1, 5, 2);
    ?>

This will print ‘is’

« « The Importance of Inheritance Within OOP
The Duties and Responsibilities of a Baker » »

Author Description

Avatar

Editorial Team at Exforsys is a team of IT Consulting and Training team led by Chandra Vennapoosa.

Free Training

RSSSubscribe 391 Followers
  • Popular
  • Recent
  • PHP Tutorial – Syntax

    May 17, 2006 - 0 Comment
  • PHP Tutorials – Operators

    September 22, 2006 - 0 Comment
  • PHP Tutorials – Data Types

    September 18, 2006 - 0 Comment
  • PHP Tutorials – Conditional Statements

    September 25, 2006 - 0 Comment
  • 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 Strings

    March 18, 2009 - 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

Exforsys e-Newsletter

ebook
 

Related Articles

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

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