Strings in PHP
Strings are always inside single or double quotation marks.
Strings can basically contain all characters, which means that strings can also contain special characters and numbers.
'1234'
is a string although it only consist of numbers. '1234'
is a string because of the
quotation marks. Just 1234
is an integer - we'll talk about that later. It is important that you use the same type of quotation mark for the beginning of the string as you do in the end. If you start a string with a double quotation mark "
you have to end it with a double quotation mark too.
Difference between double and single quotes
There are some differences between double and single quotes in PHP. Single quotes are displayed almost without parsing, which means that everything inside the single quotes will be displayed as-is. The content inside double quotes is parsed. A big advantage of this is that you can add variables inside the string.
While the first echo
will display There is 1 apple
the second one will just
display There is $variable apple
.
Escaping
If you want to use double quotes for a string inside double quotes or single quotes inside single quotes
you have to escape them because otherwise the string is invalid. You escape characters with the backslash \
.
Instead of ""Hello!""
you have to write "\"Hello!\""
. Then the string will be displayed correctly and
without any backslashes. Note that you don't have to do this when you use double inside single and the other way around.
Escaping special characters
You also have to escape the backslash with a backslash because otherwise a single backslash would try to escape the following character.
Instead of "\"
you use "\\"
. Of course the string will be displayed with only one backslash.
Inside double quotation marks you also have to escape the dollar sign $
because it marks the beginning of a variable.
As said you only have to do this inside double quotes because they're fully parsed.
Escaping examples
Isolating variables
Inside double quotes you can use curly braces to isolate the name of the variable you want evaluated.
For example when you have a string "String: $great_string"
, which will display String:
and the
value of the variable $great_string
. But what if you only need a variable called $great
. In that case you
can isolate the variable: "String: {$great}_string"
. This string will be displayed as String:
, the value of the variable
$great
and _string
.
Variable isolating exaple
Note that "test$var"
will be displayed as test
and the value of $var
.
You don't have to isolate the variable in that case.
Separating strings and other datatypes
Instead of adding a variable inside a string we can use a cleaner and clearer solution. PHP has an operator that allows
you stringing together multiple strings: It's the concatenation operator (.
).
We can connect two strings using this operator. Extra spaces between the components are ignored.
Our example will display the first string, then the variable and then the second string.
You can add as many components as you want. Later we'll also work with the other data types.
As you also can see we can use single and double quote strings.
Pretty easy, right?
The following example is a cleaner version of echo "There are $number oranges";
.
Common string commands
echo and print
As echo
is not a function but a language construct it doesn't need brackets.
A synonym for echo
is the command print
. Unlike echo
print
can also return true or false depending on the output.
trim(), ltrim() and rtrim()
Sometimes spaces left and right of a string are troublesome. For example when the string should be used for searching in texts.
Spaces can be easily removed with trim()
. Tabs and line breaks are also removed.
trim
removes the left and right spaces, rtrim
removes the right ones and ltrim
removes the left spaces.
substr()
With substr(string $str, integer $start, [integer $length])
we can extract parts from a string.
When $start
is 0 it doesn't remove anything from the left.
When $start
is a positive number it cuts off as many characters from the left as you define.
With $length
we define how many characters we go to the right until the rest is cut off.
If $length
isn't specified no characters are cut off on the right side.
Finding and replacing substrings
PHP offers a variety of functions to find substrings and replace them if necessary.
We differentiate between functions that find substrings and functions that replace them.
The functions strpos
and strstr
both belong to the first category.
strpos(string $str, string $search, [integer $startposition])
- output is an integer-
strstr(string $str, string $search)
- output is a string
The function strpos
returns the position of the first occurrence of a substring.
If the $search
substring isn't found strpos
returns false
.
An optional parameter is $startposition
. It allows you to set where the search in $str
should start.
Counting in PHP
Like in many other programming languages most functions in PHP start to count at 0
instead of 1
.
This means that the first character has the position 0
, the second one has the position 1
and so on.
strstr
is a function that searches for the first occurrence of a substring and then returns the remaining string including the needed
substring.
Finding and replacing at the same time can be done with str_replace()
.
This function can also be used for arrays but right now we'll concentrate on strings.
str_replace(string $old, string $new, string $str)
The function searches for all occurrences of $old
in $str
.
Every single occurrence is replaced with $new
. The output is the string with all replacements.
The output will be "This is new new"
.