Strings

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: 25 April 2024

The URL for this page is: http://sandersongs.com/PHPsqlCourse/PHP03.php

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:

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=2024; $month=04; $day=25;
$date = sprintf("%04d-%02d-%02d", $year, $month, $day);
echo $date; yields 2024-04-25

substr($string,start,length)

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

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 &lt; > to &gt; and & to &amp;.)
$nameLast = "<b>O'Reilly</b>";
echo htmlentities($nameLast); yields "&lt;b&gt;O'Reilly&lt;/b&gt;"

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"

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 3,535 different visits in the last 30 days.
http://sandersongs.com/PHPsqlCourse/PHP03.php
This page was last modified on our server on 11 Jul 2021
and last refreshed on our server at 2:49 pm, UTC
This file took 0.00924 seconds to process.