Friday, April 19

PHP String Length

The handling of strings is one of the pillars of PHP programming, and although there are countless functions to work with that can be consulted in the official programming, here we will only see the most used ones.

Strings can be defined in different ways, although the most common is to enclose them in quotes (single or double). In principle, both forms are accepted, although a small classification is established:

  • We will use single quotes for strings.
  • We will use double quotes if we are going to use escape characters or control interpolate variables, etc.

For example:

echo “\ x2a”; // display an asterisk using hex notation

$who = “María”;

echo “Hello $who \ n”; // show Hello María and change lines

echo ‘Hello $who \ n’; // show Hello María \ n

echo ‘My name is \’ María \ ‘.’; // Show My name is ‘María’.

$who = “María”;

echo “The value of the variable \ $who is \” $who \ “.”; // Show the value of the variable $who is “María”.

To know the length of a string we use the strlen () function, which returns the length in bytes of the string, which implies that all characters count, regardless of their value.

For example:

echo strlen (“María”); // returns 5

echo strlen (“María Smith”); // returns 11

The strtr () function allows transforming a string, replacing some characters with predefined ones. For example:

echo strtr (‘María’, ‘i’, ‘1’); // returns Mar1a

$ numbers = [‘i’ => ‘1’, ‘o’ => ‘0’];

echo strtr (“María”, $numbers); // returns Mar1a

Also, it is possible to use a string as if it were an array in which each character is an element of it. For example:

$who = ‘María’;

echo $who [1]; // returns to

for ($i = 0; $i <strlen ($who); $ i ++) {

echo $who [$i]. “-“; // returns M-a-r-í-a-}

 

Compare Strings

Sometimes we will need to compare strings to perform certain operations, although we must be careful with internal conversions between PHP data. Thus, the following example will return true, since internally the string will be converted to the integer 123 before the comparison. This could have been avoided if we had used the identical === comparer, for example:

$string = ‘123abc’;

echo ($string == 123); // returns true

echo ($string === 123); // returns false

However, there are specific PHP functions to perform the comparisons and the whose return value will be 0 if the strings are considered equal. These functions are:

  • strcmp (), which compares two strings as is, as if we were using the identical operator
  • strcasecmp (), which works like the one above, but is case insensitive. Very useful for captcha fields, email, etc.
  • strncmp () and strncasecmp (), which allow you to compare only a defined number of characters (in the second case in a case-insensitive way). A function similar to this would be substr_compare ()

$string = ‘Hello María’;

echo strcmp ($string, ‘Hi María’); // returns -32, implies that they are not equal

echo strcasecmp ($string, ‘Hi María’); // returns 0, implies that they are the same because it is case insensitive

echo strncmp ($string, ‘Hello María’, 5); // returns 0, it implies that they are equal because I only compare the first 5 characters

Besides those mentioned above, there are many more functions in PHP to work with strings. All of them are available in the official PHP documentation.