ComputersProgramming

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.

Any algorithm is a sequence of choices and actions. But before making a choice, you need to compare something with something. Lines - 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 dynamic typing language does not make much difference 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'.

However, there is a difference between using is_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).

This simple example is a good example, as PHP's string comparison operators ('==', '===', '! =', ...) can present many 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.

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 right up to regular expressions.

Boundaries of available functionality

PHP-comparison of two strings is well "done" by the function strpos () - 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 the other.

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

If the call to $ cResult = scCheckFileName ($ cStr) yields 'true', then the string is the name of the vordian file. He will have only one option for the extension '.docx' and no characters in the name: only letters, numbers and '_', '-' signs.

The function can easily be converted to other kinds of files: $ cPtr = '/^([a-zA-Z...0-9\-\_]{4,239})\.(html|js|css|png|jpg | Docx | txt) {1} $ / u '. This option checks the string extends the range of downloadable (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, ...

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

PHP Functionality: string comparison

Arsenal of language versus strings is not only a function of pure comparison, but a combination with searching or replacing directly. Not always the action should coincide with the comparison, because the latter does not necessarily lead to a change of 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 first line enters the second line, from which the PHP function (string comparison) decides. A more limited version of strpos (), since in the latter case you can know the entry position.

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

In practice, often it is required to work not with the whole line, but only with its part. To do this, the set of PHP functions (string comparison) includes strncmp (s1, s2, N). 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.

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

Then you can perform 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'll have to sort through the array elements. It takes longer and looks more cumbersome.

Objects and strings

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

The modern concept of objects assumes that they have properties and methods. The first ones are usually represented by numbers, strings, arrays and other objects. The second often includes the methods of writing (put) in a string and restoring from a string (get).

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

This moment gives the basis and possibility at record to fix in a line only the necessary information, and at restoration from a line to restore all working properties in the necessary state. Usually in any object there is information essential and working (temporary). Implementing this idea allows you to save not only memory, disk space, database records, but also makes it possible to compare strings in a more simple and precise way.

Syntax and semantics

PHP dynamically develops, and its functionality both in terms of string comparison and in terms of processing them is constantly being improved. However, nothing prevents the developer from transferring the center of gravity to the field of semantics.

Undoubtedly, the functional is good, but its use can be transferred to the semantic part of the code, into objects. When the algorithm is represented as a system of interaction of objects, it looks much better than a sequence of comparisons and actions in direct serial, classical style.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

Copyright © 2018 en.unansea.com. Theme powered by WordPress.