Strings in PHP
Strings are sets of ordered characters that make up text.
Strings are normally contained within
" " or
The difference between the single quotation mark (or apostrophe) and the double quotation marks (speech marks) is that variables within the single quotation marks (') will not be parsed as variables and will be output directly as they are. In double quotation marks (") the variable will be parsed:
<?php $x = 10; echo "$x is $x"; echo "<br />; echo '$x is $x'; ?>
$x is $x
Strings can be manipulated in many different ways
such as with other programming languages such as Java. For instance, the
substring method will get a smaller
inner string from within a string. In PHP, there are several generic
Replace a string inside a string
When a string has text that is to be replaced,
str_replace replaces a string
within a string and returns it. The following example will replace
the word "website" with "dynamic website" within the string specified
to it and stores it in
<?php $myStr = "Welcome to my website"; $test = str_replace("website", "dynamic website", $myStr); ?>
Get the length of a string
To get the length of some string the
function can be used. This
takes in a string as a parameter and returns the length of the string.
<?php $myStr = "Welcome to my website"; $test = strlen($myStr); ?>
Getting the index of the first occurence of a string
To get the index of the first occurence of a string within
a string, the
is used. It is given a parameter of the string to search then
the string to search for.
<?php $myStr = "Welcome to my website"; $test = strpos($myStr, "website"); ?>
In this case, this will return the first index where the word "website" appears.
Getting a substring from a string
A substring is a string within a string. That is means in
logic, we can state that A ⊂ B.
In PHP we can use the
To get a substring from the string "My name
is Jamie" the
substr method can be used to get just my "Jamie". If
"Jamie" begins on character 12, then the first letter is
on character 11, taking into account Base 0 counting.
<?php $myStr = "My name is Jamie"; $test = substr($myStr, 11, 5); ?>
substr takes in three parameters, one which contains the
string to substring, one that specifies where to begin the substring
from and an optional length. So the last parameter was set to
3 it would return "Jam" rather than "Jamie". The last parameter
is optional, so if it is not included it takes from the
start to the end.
Encoding a string
MD5 and SHA1 are both string hash algorithms that allow the encoding of a string for use in URLs, order numbers or to store a unique reference in a database or on a web server. Both of these work differently under the hood. Both are shown below.
It is beyond the scope of this article to describe the benefits of MD5 or SHA1, but for the sake of web programming in PHP, it is necessary to know how to store information correctly.
MD5 hashing algorithm
<?php $myStr = "My name is Jamie"; $test = md5($myStr); ?>
SHA-1 (Secure Hash Algorithm)
<?php $myStr = "My name is Jamie"; $test = sha1($myStr); ?>
Concatenating two strings
Strings are merged together in PHP using the
concatenation operator. In Java, this operator is
VB.NET this operator is
&. In PHP the
. (full stop) is
the concatenation operator.
Two strings can be joined using the following:
<?php $test = "My hello world " . " is different to yours."; ?>
Getting a character from a string position
Strings are a set of ordered characters. As this is the case it is sometimes necessary to obtain a certain character from such a string. Getting an index from a string is done using the square brackets like this:
<?php $myStr = "My name is Jamie"; $test = $myStr; ?>
Where the 4 inside the square brackets is the index.