Strings in PHP with examples

A string is a group of characters that can represent text or other data in PHP. A series of characters can be turned into a string by enclosing them in single ('...') or double quotes ("..."). Here are a few instances:

$name = 'William';
$city = "Houston";

PHP string example

Consider the following PHP code as an example demonstrating the string.

PHP Code
<?php 
   $name = 'William';
   echo $name;
   echo "<BR>";
   
   $city = "Houston";
   echo $city;
?>
Output
William
Houston

In the following PHP code, two string variables, $name and $city, are created, respectively given the values "William" and "Houston," and then printed using the echo statement.

The opening tag for PHP code appears in the first line, <?php. The string value 'William' is assigned to the variable $name in the second line. The echo statement is used in the third line to print the value of $name. "William" will be the output.

The echo statement is used to print an HTML line break (<BR>) in the fourth line.

The string value "Houston" is given to the variable $city in the fifth line. The echo statement is used to print the value of $city in the sixth line. Houston will appear as the output.

The closing tag ?>, which denotes the end of the PHP code block.

Find the length of a string in PHP

To find the length of a string in PHP, use the "strlen()" function. Consider the following PHP code as an example:

PHP Code
<?php 
   $mystr = "Hello there";
   $len = strlen($mystr);
   echo $len;
?>
Output
11

And to count the number of words in a string, use the "str_word_count()" function. As an example:

PHP Code
<?php 
   $mystr = "Hello there";
   $len = str_word_count($mystr);
   echo $len;
?>
Output
2

Search a substring in a string in PHP

The "strpos()" function in PHP can be used to find the position of a needed substring in a string. As an example:

PHP Code
<?php 
   $mystr = "Hello there, I'm William, from Houston, Texas.";
   $mywrd = "Houston";
   
   $p = strpos($mystr, $mywrd);
   echo $mywrd . " is found at position " . $p . ".";
?>
Output
31

Because indexing starts with 0, therefore

In this way, you can also find some patterns of substrings in a string.

PHP Code
<?php 
   $mystr = "Hello there, I'm William, from Houston, Texas.";
   $mywrd = ", T";
   
   $p = strpos($mystr, $mywrd);
   echo "\"" . $mywrd . "\" is found at position " . $p . ".";
?>
Output
", T" is found at position 38.

In the above example, I used (\") to print a double quote (").

Replace a substring from a string in PHP

To replace a substring from a string in PHP, you can use the "str_replace()" function.

PHP Code
<?php 
   $mystr = "Hello there, I'm William, from Houston, Texas.";
   $my_old_wrd = ", T";
   $my_new_wrd = "-T";
   
   $my_new_str = str_replace($my_old_wrd, $my_new_wrd, $mystr);
   echo $my_new_str;
?>
Output
Hello there, I'm William, from Houston-Texas.

Create a string spanning multiple lines in PHP

To create and initialize a string to a variable in PHP that spans multiple lines without using the concatenation operator or escapse sequences, you can use the PHP heredoc syntax. Consider the following PHP code as an example:

PHP Code
<?php 
   $name = 'William';
   $city = "Houston";
   $state = "Texas";
   
   $text = <<<EOT
      Hi, I'm $name.
      I live in $city, $state.
      Where do you live?
   EOT;
   
   echo $text;
?>
Output
Hi, I'm William. I live in Houston, Texas. Where do you live?

This PHP code creates three string variables $name, $city, and $state, assigns them the respective values 'William,' 'Houston,' and 'Texas,' and then defines a heredoc string variable $text that contains these values. Lastly, the value of $text is printed using the echo statement.

PHP Online Test


« Previous Tutorial Next Tutorial »