PHP practice: string comparison

PHP is pretty good at information processing. The language syntax is represented by a full-featured set of functions for comparing, processing strings, and comparison operators.

php string comparison

Any algorithm is a sequence of choices and actions. But before you make a choice, you need to compare something with something. Strings are the most capacious, efficient and practical mechanism for controlling the algorithm. Rows - a variant of data representation. And data is the main subject of "concern" of any algorithm.

Conventional comparison-action logic

In general, the dynamic typing language does not make much difference in the data, for example, in PHP, comparing strings and numbers is not much different. A number is also a string when it contains only numbers, a period, and there is not a single character that is not used to represent a number in any form (mathematical notation).

In the case of kontenenatsii, the number automatically merges with the string without unnecessary questions and no hidden errors, even if the gettype () function gives 'integer' or 'double'.

php compare two lines

However, there is a difference between using the is_int () and is_numeric () functions. The first gives true when the parameter is only an integer, the second when any number or numeric string (the variable is of type 'string', but contains everything that is provided by mathematical notation).

This simple example is a good example of how string comparison operators ('==', '===', '! =', ...) in PHP can bring a lot of surprises. Variables can change their type; they are not always numbers, but almost always they can be cast to a string. In extreme cases, this will be an empty string.

php string comparison operators

Based on the above, in PHP the string comparison function is the most popular. Which one to choose, the developer to decide. A lot of options are available up to regular expressions.

The boundaries of the available functionality

The PHP comparison of two strings is well done by the strpos () function, which is the cheapest, true, and practical option. If the result of this function is a number, then one line is unambiguously equal to another or one line is included in another.

The fundamentally opposite, but also absolutely correct approach is the use of regular expressions.

Author's example is not a picture

If calling $ cResult = scCheckFileName ($ cStr) gives 'true', then the line is the name of the Word file. It will have only one extension option '.docx' and no characters in the name: only letters, numbers and signs '_', '-'.

The function can easily be redone for other types of files: $ cPtr = '/^([a-zA-Z...0-9\-\_†{4,239})\.(html|js|css|png|jpg | docx | txt) {1} $ / u '. This type of line check extends the range of loaded ones (for example, in PHP string comparison is used "to upload files to the server, without any chance of an input error") on html, js, css, ...

Using strpos () and preg_match () are extremes. They are not directly related to the issue of term comparison. But the question of the algorithm is the question of applying a combination of styles, using all the possibilities to achieve a reliable and correct result.

PHP functionality: string comparison

The arsenal of language for comparing strings is not only pure comparison functions, but also a combination with searching or replacing directly. Not always the action should coincide with the comparison, since the latter does not necessarily lead to a change in any line. Often you need to choose one or another branch of the algorithm.

The usual PHP version: string comparison is performed by the int strcmp (s1, s2) function.

Function Result:

  • 0 - lines are equal;
  • -1 - the first line is less than the second;
  • 1 - the first line is larger than the second.

In practice, this means that the first line enters the second, from which the PHP function (string comparison) makes a decision. A more limited version of strpos (), since in the latter case you can know the position of the entry.

The strcmp () function is case sensitive. If you need to compare strings case-insensitive, PHP suggests using strcasecmp (). The syntax is similar.

In practice, it is often required to work not with the entire string, but only with its part. To do this, the PHP function set (string comparison) includes strncmp (s1, s2, N). The third parameter tells you to compare only N-bytes. The result is similar to strcmp ().

Arrays, Strings, and Comparison

Data is almost always represented by rows. If we consider arrays, objects, or information structures, then these are simply different options for combining simpler string structures.

php string comparison function

String arrays and strings can be represented in complementary ways. Transformation of an array into a string using the implode (array, symbol) function, for example: $ margins1 = implode (',', $ style-> getInnerMargin ()); ... algorithm / user operation ...; $ margins2 = implode (',', $ style-> getInnerMargin ()) allows you to merge all the positions of the object into a line of positions.

Then you can do a PHP string comparison at a time: $ check = strcmp ($ margins1, $ margins2) and make sure that the algorithm or user has changed something (or not). If you perform the comparison in the usual way, you will have to sort through the elements of arrays. This takes longer and looks more bulky.

Objects and Strings

An even more effective use of PHP (string comparison) can be realized through object-oriented ideas.

The modern understanding of objects implies the presence of properties and methods. The former are usually represented by numbers, strings, arrays, and other objects. The second often includes methods for writing (put) to a string and restoring from a string (get).

Unlike arrays, an object does work with its properties and interacts with other objects. The object is "competent" in what its properties are of real importance for the algorithm, the program as a whole.

php string and number comparison

This moment gives the basis and the ability to record only the necessary information in the string when writing, and when restoring from the string, restore all working properties to the desired state. Usually in any object there is significant and operational information (temporary). The implementation of such an idea not only saves memory, disk space, database records, but also makes it possible to compare strings with simpler and more accurate means.

Syntax and semantics

PHP is developing dynamically, and its functionality, both in terms of comparing strings and regarding their processing, is constantly being improved. However, nothing prevents the developer from moving the center of gravity to the field of semantics.

Sure, the functionality is good, but its use can be transferred to the semantic part of the code, to objects. When an algorithm is presented as a system of interaction of objects, it looks much better than a sequence of comparisons and actions in a direct, sequential, classical style.

Source: https://habr.com/ru/post/K12560/


All Articles