Strings are variables designated as text. They can contain a single letter, a group of letters, words, paragraphs, books, characters and even numbers. If the data is defined using quotation marks (either single or double quotes), it is considered a string.
You can get undesired results if you are careless with quotes. For instance, if $this = "Dave said "Hi!" before he left."; it will cause a Parse Error. PHP thinks you want $this = "Dave said "; but it can't find the ending semi-colon it expected. One solution is to use different quotes, like so: $this = 'Dave said "Hi!" before he left.'; . Now it appears as expected.
There are many ways you can manipulate strings. One of the most popular is concatenation. This is where you combine two or more strings together to make one larger string. The period ( . ) is used by PHP to combine strings. For instance:
which displays thus:
The URL for this page is: http://sandersongs.com/PHPsqlCourse/PHP03.php
Mark F. Jones
- Here we actually combined five strings--
- $nameFirst,
- a string with a space,
- $nameMI,
- a string with a period and a space, and
- $nameLast.
Note that a string with a space is not the same as an empty string. If $a = ""; and $b = " "; then $a does not equal $b.
String Functions
PHP has many built-in functions to manipulate strings. Here are some of the more popular ones:
strtolower($string)
Returns string with all alphabetic characters converted to lowercase.
$nameLast = "O'Reilly";
echo strtolower($nameLast); yields "o'reilly"
strtoupper($string)
Returns string with all alphabetic characters converted to uppercase.
$nameLast = "O'Reilly";
echo strtoupper($nameLast); yields "O'REILLY"
trim($string)
Strip whitespace from the beginning and end of $string. Similarly, ltrim($string) strips whitespace from only the left side of $string and rtrim($string) strips whitespace from only the right side of $string.
$response = " stop ";
echo trim($response); yields "stop"
echo ltrim($response); yields "stop "
echo rtrim($response); yields " stop"
number_format(number,decimal_places)
Returns a formatted version of number, complete with commas and decimal points, rounded to the specified decimal place. The returned formatted number is now a string.
echo number_format(1234567.8876,2); yields 1,234,567.89
explode('separator',$string)
Returns an array of strings, each of which is a substring of $string formed by splitting it on boundaries formed by the string separator. (Makes an array out of a string.)
$fullName = "Mark F. Jones";
$nameParts = explode(" ",$fullName);
echo $nameParts[0]; yields "Mark"
implode('glue',$array)
Returns a string containing all $array elements in the same order, with the glue string between each element. (Makes a string out of an array.)
$nameParts = array('Mark','F','Jones');
$fullName = implode(" ",$nameParts);
echo $fullName; yields "Mark F Jones"
sprintf(format,$string)
Returns a string produced according to the formatting format.
$year=2025; $month=02; $day=11;
$date = sprintf("%04d-%02d-%02d", $year, $month, $day);
echo $date; yields 2025-02-11
substr($string,start,length)
Returns the portion of string specified by the start and length parameters.
$date = "2025-02-11";
substr($date,5,2); yields "02"
str_replace('find','replace',$string)
Returns a string or an array with all occurences of find in $string replaced with replace.
$content = "I would never lie about this!";
echo str_replace('lie','lie again',$content); yields I would never lie again about this!
str_pad($string,length,'padding','where')
Returns $string padded on the left, the right, or both sides to the specified padding length.
$slogan = "Over Here";
echo str_pad($slogan,15,'-->',STR_PAD_LEFT); yields -->-->Over Here
strip_tags($string)
Strip HTML and PHP tags from a string
$nameLast = "<b>O'Reilly</b>";
echo strip_tags($nameLast); yields "O'Reilly"
htmlentities($string)
Convert all applicable characters to HTML entities. (Changes < to < > to > and & to &.)
$nameLast = "<b>O'Reilly</b>";
echo htmlentities($nameLast); yields "<b>O'Reilly</b>"
nl2br($string)
Returns $string with '<br />' inserted before all newlines. Use it when you retrieve the text from the database to output the text for the the webpage. If you use nl2br in conjunction with htmlentities, you should process the string with nl2br after htmlentities to prevent htmlentities from escaping your newly generated <br /> symbols.
addslashes($string)
Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote ('), double quote ("), backslash (\) and NUL (the NULL byte).
$nameLast = "O'Reilly";
echo addslashes($nameLast); yields "O\'Reilly"
stripslashes($string)
Returns a string with backslashes stripped off. (\' becomes ' and so on.) Double backslashes (\\) are made into a single backslash (\).
$nameLast = "O'Reilly";,
$slashName = addslashes($nameLast);, echo $slashName;yields "O\'Reilly"
$stripName = stripslashes($slashName);, echo $stripName; yields "O'Reilly"