• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Matt Doyle | Elated Communications

Web and WordPress Development

  • About Me
  • Blog
    • Design & Multimedia
      • Photoshop
      • Paint Shop Pro
      • Video & Audio
    • Online Marketing
      • E-Commerce
      • Social Media
    • Running a Website
      • WordPress
      • Apache
      • UNIX and Linux
      • Using FTP
    • Web Development
      • HTML
      • CSS
      • JavaScript
      • PHP
      • Perl and CGI Scripting
  • Portfolio
  • Contact Me
  • Hire Me
Home / Blog / Web Development / PHP / PHP Strings / Basic PHP String Operations

Basic PHP String Operations

28 September 2009 / Leave a Comment

Now that you know how to create strings in PHP, it’s time to try out some basic string operations. In this tutorial you learn how to:

  • Join strings together
  • Find out the length of a string, and
  • Read and change individual characters within a string.

Joining strings

To join 2 or more strings together to make 1 long string, use the concatenation operator, which is a . (dot). For example, to join 2 strings you could use:


$string1 = 'Hello, ';
$string2 = 'there!';
$string3 = $string1 . $string2;
echo $string3; // Displays "Hello, there!" 

To join 3 strings you might write:


$string1 = 'Hello,';
$string2 = ' ';
$string3 = 'there!';
$string4 = $string1 . $string2 . $string3;
echo $string4; // Displays "Hello, there!" 

Getting the length of a string

You can use PHP’s strlen() function to retrieve the number of characters in a string:


$myString = 'Hello, there!';
echo strlen( $myString ); // Displays "13" 

If you want to find out the number of words in a string, you can use str_word_count():


$myString = 'Hello, there!';
echo str_word_count( $myString ); // Displays "2"

Accessing characters within a string

To access a character in a string, write the string variable name followed by the character position, or index, in square brackets:


$myString = 'Hello, there!';
echo $myString[7];  // Displays "t"
String indices start from zero. The first character in a string has an index of 0, the second has an index of 1, and so on.

You can change characters in a string using the same technique:


$myString = 'Hello, there!';
$myString[12] = '?';
echo $myString;  // Displays "Hello, there?"

If you want to read a sequence of characters from a string, use PHP’s substr() function. This takes the following arguments:

  • The string.
  • The position to start extracting characters. Use a negative number to count backwards from the end of the string.
  • The number of characters to extract (optional). Miss out this argument to extract from the start position to the end of the string. You can also supply a negative number to miss out that many characters from the end of the string.

Here are some substr() examples:


$myString = 'Hello, there!';
echo substr( $myString, 7 ) . "<br />";       // Displays "there!"
echo substr( $myString, 0, 5 ) . "<br />";    // Displays "Hello"
echo substr( $myString, -4 ) . "<br />";      // Displays "ere!"
echo substr( $myString, -6, -1 ) . "<br />";  // Displays "there"
You can’t use substr() to change characters within strings; however you can do this with PHP’s substr_replace() function.

You now know how to carry out basic operations on PHP strings. You’ve looked at joining strings together; how to find out the length of a string; and how to work with individual characters within a string. Happy coding!

Filed Under: PHP Strings Tagged With: character position, index, join php strings, php get length of string, square brackets, str_word_count, strlen, substr, substr_replace

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

To include a block of code in your comment, surround it with <pre> ... </pre> tags. You can include smaller code snippets inside some normal text by surrounding them with <code> ... </code> tags.

Allowed tags in comments: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre> .

Primary Sidebar

Hire Matt!

Matt Doyle headshot

Need a little help with your website? I have over 20 years of web development experience under my belt. Let’s chat!

Matt Doyle - Codeable Expert Certificate

Hire Me Today

Call Me: +61 2 8006 0622

Stay in Touch!

Subscribe to get a quick email whenever I add new articles, free goodies, or special offers. I won’t spam you.

Subscribe

Recent Posts

  • Make a Rotatable 3D Product Boxshot with Three.js
  • Speed Up Your WordPress Website: 11 Simple Steps to a Faster Site
  • Reboot!
  • Wordfence Tutorial: How to Keep Your WordPress Site Safe from Hackers
  • How to Make Awesome-Looking Images for Your Website

Footer

Contact Matt

  • Email Me
  • Call Me: +61 2 8006 0622

Follow Matt

  • E-mail
  • Facebook
  • GitHub
  • LinkedIn
  • Twitter

Copyright © 1996-2023 Elated Communications. All rights reserved.
Affiliate Disclaimer | Privacy Policy | Terms of Use | Service T&C | Credits