- PHP Basics
- Learn PHP
- PHP Comments
- PHP Data Types
- PHP Variables
- PHP Operators
- PHP echo
- PHP print
- PHP echo vs. print
- PHP if else
- PHP switch
- PHP for Loop
- PHP while Loop
- PHP do...while Loop
- PHP foreach Loop
- PHP break and continue
- PHP Arrays
- PHP print_r()
- PHP unset()
- PHP Strings
- PHP Functions
- PHP File Handling
- PHP File Handling
- PHP Open File
- PHP Create a File
- PHP Write to File
- PHP Read File
- PHP feof()
- PHP fgetc()
- PHP fgets()
- PHP Close File
- PHP Delete File
- PHP Append to File
- PHP Copy File
- PHP file_get_contents()
- PHP file_put_contents()
- PHP file_exists()
- PHP filesize()
- PHP Rename File
- PHP fseek()
- PHP ftell()
- PHP rewind()
- PHP disk_free_space()
- PHP disk_total_space()
- PHP Create Directory
- PHP Remove Directory
- PHP Get Files/Directories
- PHP Get filename
- PHP Get Path
- PHP filemtime()
- PHP file()
- PHP include()
- PHP require()
- PHP include() vs. require()
- PHP and MySQLi
- PHP and MySQLi
- PHP MySQLi Setup
- PHP MySQLi Create DB
- PHP MySQLi Create Table
- PHP MySQLi Connect to DB
- PHP MySQLi Insert Record
- PHP MySQLi Update Record
- PHP MySQLi Fetch Record
- PHP MySQLi Delete Record
- PHP MySQLi SignUp Page
- PHP MySQLi LogIn Page
- PHP MySQLi Store User Data
- PHP MySQLi Close Connection
- PHP Misc Topics
- PHP Object Oriented
- PHP new Keyword
- PHP Cookies
- PHP Sessions
- PHP Date and Time
- PHP GET vs. POST
- PHP File Upload
- PHP Image Processing
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 $name = 'William'; echo $name; echo "<BR>"; $city = "Houston"; echo $city; ?>
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 $mystr = "Hello there"; $len = strlen($mystr); echo $len; ?>
11
And to count the number of words in a string, use the "str_word_count()" function. As an example:
<?php $mystr = "Hello there"; $len = str_word_count($mystr); echo $len; ?>
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 $mystr = "Hello there, I'm William, from Houston, Texas."; $mywrd = "Houston"; $p = strpos($mystr, $mywrd); echo $mywrd . " is found at position " . $p . "."; ?>
31
Because indexing starts with 0, therefore
- 'H' is at 0
- 'e' is at 1
- 'l' is at 2
- 'l' is at 3
- 'o' is at 4
- ' ' (a space) is at 5
- 't' is at 6
- :
- 'H' (the first letter of "Houston") is at 31
In this way, you can also find some patterns of substrings in a string.
<?php $mystr = "Hello there, I'm William, from Houston, Texas."; $mywrd = ", T"; $p = strpos($mystr, $mywrd); echo "\"" . $mywrd . "\" is found at position " . $p . "."; ?>
", 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 $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; ?>
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 $name = 'William'; $city = "Houston"; $state = "Texas"; $text = <<<EOT Hi, I'm $name. I live in $city, $state. Where do you live? EOT; echo $text; ?>
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.
« Previous Tutorial Next Tutorial »