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:

<?php $today = date('j F Y'); ?> Today's date is: <?php echo $today; ?><br /><br /> The URL for this page is: <?php // This comment won't appear on the page or affect the code in any way echo 'http://'.$_SERVER["SERVER_NAME"].$_SERVER["PHP_SELF"]; echo "<br /><br />"; $nameLast = "Jones"; $nameFirst = "Mark"; $nameMI = "F"; $fullName = $nameFirst . " " . $nameMI . ". " . $nameLast; echo $fullName; ?>

which displays thus:

Today's date is: 13 July 2024

The URL for this page is:

Mark F. Jones
    Here we actually combined five strings--
  1. $nameFirst,
  2. a string with a space,
  3. $nameMI,
  4. a string with a period and a space, and
  5. $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:


Returns string with all alphabetic characters converted to lowercase.
$nameLast = "O'Reilly";
echo strtolower($nameLast); yields "o'reilly"


Returns string with all alphabetic characters converted to uppercase.
$nameLast = "O'Reilly";
echo strtoupper($nameLast); yields "O'REILLY"


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"


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


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"


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"


Returns a string produced according to the formatting format.
$year=2024; $month=07; $day=13;
$date = sprintf("%04d-%02d-%02d", $year, $month, $day);
echo $date; yields 2024-07-13


Returns the portion of string specified by the start and length parameters.
$date = "2024-07-13";
substr($date,5,2); yields "07"


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!


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 HTML and PHP tags from a string
$nameLast = "<b>O'Reilly</b>";
echo strip_tags($nameLast); yields "O'Reilly"


Convert all applicable characters to HTML entities. (Changes < to &lt; > to &gt; and & to &amp;.)
$nameLast = "<b>O'Reilly</b>";
echo htmlentities($nameLast); yields "&lt;b&gt;O'Reilly&lt;/b&gt;"


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.


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"


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"

Variables   < <  PREVIOUS   Table of Contents NEXT  > >   Arrays

Developed with HTML-Kit
Sandersongs Web Tutorials
Contact the Webmasterwith comments.
©2024, by Bill Sanders, all rights reserved.
This domain had 349 different visits in the last 30 days.
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 3:11 am, UTC
This file took 0.00776 seconds to process.