Strings in PHP – Free PHP Tutorials

Strings in PHP can be handle easily for real life application.In programming, strings are concatenations of characters.

PHP brings a whole set of predefined functions that help you in interacting with strings. You can find the entire list of functions at http://php.net/manual/en/ref.strings.php, but we will only cover the ones that are used the most.

Let’s look at some examples:

<?php
$text = ' How can a clam cram in a clean cream can? ';
echo strlen($text); // 45
$text = trim($text);
echo $text; // How can a clam cram in a clean cream can?
echo strtoupper($text); // HOW CAN A CLAM CRAM IN A CLEAN CREAM CAN?
echo strtolower($text); // how can a clam cram in a clean cream can?
$text = str_replace('can', 'could', $text);
echo $text; // How could a clam cram in a clean cream could?
echo substr($text, 2, 6); // w coul
var_dump(strpos($text, 'can')); // false
var_dump(strpos($text, 'could')); // 4

In the preceding long piece of code, we are playing with a string with different functions:

  1. strlen: This function returns the number of characters that the string contains.
  2. trim: This function returns the string, removing all the blank spaces to the left and to the right.
  3. strtoupper and strtolower : These functions return the string with all the characters in upper or lower case respectively.
  4. str_replace: This function replaces all occurrences of a given string by the replacement string.
  5. substr: This function extracts the string contained between the positions specified by parameters, with the first character being at position 0.
  6. strpos: This function shows the position of the first occurrence of the given string. It returns false if the string cannot be found.

Concatenation Operator(.) in PHP

Additionally, there is an operator for strings (.) which concatenates two strings (or two variables transformed to a string when possible). Using it is really simple: in the following example, the last statement will concatenate all the strings and variables forming the sentence, I am Hiro Nakamura!.

<?php
$firstname = 'Hiro';
$surname = 'Nakamura';
echo 'I am ' . $firstname . ' ' . $surname . '!';

Another thing to note about strings is the way they are represented. So far, we have been enclosing the strings within single quotes, but you can also enclose them within double quotes. The difference is that within single quotes, a string is exactly as it is represented, but within double quotes, some rules are applied before showing the final result. There are two elements that double quotes treat differently than single quotes: escape characters and variable expansions.

Escape characters: These are special characters than cannot be represented easily. Examples of escape characters are new lines or tabs. To represent them, we use escape sequences, which are the concatenation of a backslash
(\) followed by some other character. For example, \n represents a new line, and \t represents a tabulation.

Variable expanding: This allows you to include variable references inside the string, and PHP replaces them by their current value. You have to include the $ sign too.

Have a look at the following example:

<?php
$firstname = 'Hiro';
$surname = 'Nakamura';
echo "My name is $firstname $surname.\nI am a master of time and
space. \"Yatta!\"";

The preceding piece of code will print the following in the browser:

My name is Hiro Nakamura.
I am a master of time and space. "Yatta!"

Here, \n inserted a new line. \” added the double quotes (you need to escape them too, as PHP would understand that you want to end your string), and the variables $firstname and $surname were replaced by their values.

Leave a Comment

Your email address will not be published. Required fields are marked *