/ / PHP Practice: string comparison

Practice PHP: string comparison

PHP is quite good at information processing. The syntax of the language is represented by a full-featured set of comparison functions, string processing, comparison operators.

php string comparison

Any algorithm is a sequence of choices andactions. But before making a choice, you need to compare something with something. Strings - the most capacious, effective and practical mechanism for controlling the algorithm. Lines - a variant of data representation. And the data is the main subject of "care" of any algorithm.

The usual logic of "comparison-action"

In general, the language of dynamic typing is notmakes special differences in the data, for example, in PHP, the comparison of a string and a number is not much different. A number is also a string when it contains only digits, a period, and there is not a single character that is not used to represent a number in any of its forms (mathematical notation).

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

php comparison of two strings

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

Этот простой пример - хороший образец, как на PHP string comparison operators ("==", "===", "! =", ...) can give a lot of surprises. Variables can change their type, they are not always numbers, but almost always they can lead to a string. In an extreme case, 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, to solve the developer. A lot of options are available up to regular expressions.

Boundaries of available functionality

PHP-comparison of two strings is well "done" by the functionstrpos () is the cheapest, right and practical option. If the result of this function is a number, then uniquely one line is equal to the other or one enters another.

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

The author's example is not a picture

Если вызов функции $cResult = scCheckFileName ($ cStr) will give "true", so the string is the name of the vordian file. He will have only one extension option ".docx" and no characters in the name: only letters, numbers and "_", "-" signs.

The function can easily be converted to othertypes of files: $ cPtr = "/^([a-zA-Z...0-9-_]{4,239}).(html|js|css|png|jpg|docx|txt ){1}$/ u ". Such a string validation option extends the range of downloadable ones (for example, in PHP, the string comparison is applied "for uploading files to the server, without a single chance of an input error") on html, js, css, ...

Использование strpos () и preg_match () - extremes. They are not directly related to the issue of timing 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.

Functional PHP: string comparison

Arsenal of language versus lines is not onlyfunctions of pure comparison, but also combination with search or replacement directly. Not always the action should coincide with the comparison, because the latter does not necessarily lead to a change in any line. It is often necessary to select one or another branch of the algorithm.

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

Function Result:

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

In practice, this means that the firstThe second line, from which the PHP function (string comparison) decides. A more limited version of strpos (), because in the latter case you can know the entry position.

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

In practice, it is often necessary to work not with the wholeline, but only with its part. For this, strncmp (s1, s2, N) is included in the set of PHP functions (string comparison). The third parameter indicates that only N-bytes are compared. The result is similar to strcmp ().

Arrays, Strings, and Comparisons

Data is almost always represented by strings. If we consider arrays, objects, or information structures, then these are simply different variants of a combination of simpler string structures.

php string comparison function

String arrays and strings can bepresented in a complementary way. Transforming an array into a string with the function implode (array, symbol), for example: $ margins1 = implode (",", $ style-> getInnerMargin ()); ... the work of the algorithm / user ...; $ margins2 = implode (",", $ style-> getInnerMargin ()) allows you to merge all the positions of an object into a string of positions.

Then you can run PHP-string comparison and foronce: $ 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, then you have to sort through the elements of the arrays. It takes longer and looks more cumbersome.

Objects and Strings

Even more dramatic use of PHP (string comparison) can be implemented through object-oriented ideas.

Modern view of objectsassumes that they have properties and methods. The former are usually represented by numbers, strings, arrays, and other objects. The latter often include methods of writing (put) to a string and recovering from a string (get).

В отличие от массивов, объект выполняет работу со its properties and interacts with other objects. The object is "competent" in what its properties have a real meaning for the algorithm, the program as a whole.

php string and number comparison

This moment gives ground and opportunity whenwrite records to a string only the necessary information, and when restoring from a string, restore all working properties to the desired state. Usually in any object there is information essential and working (temporary). Implementing such an idea saves not only 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 is as inIn terms of comparing strings, and with regard to their processing is constantly being improved. However, nothing prevents the developer from shifting the center of gravity to the domain of semantics.

Sure, the functional is good, but itsuse can be transferred to the meaning of the code in the 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.