Using the explode PHP function

The semantics of the development of programming languages ​​and the scope of their applications quickly took the right positions. Of course, mathematical calculations of the trajectories of the planets and the movement of exchange rates in the markets still excite society, but even there strings of symbols occupy a worthy place.

explode php

An essential point: from strict typing of variables to their absolutely free use and automatic type conversion, the path was much longer as needed.

Everything in the world has lines

One can argue with this, but there is no special meaning, and even more so in programming. The semantics of explode PHP greatly simplified the work of the programmer: she transferred the use of several functions and constructs into one whole.

At the same time, the syntax and semantics of PHP itself with respect to this function provide the developer with the opportunity to come up with their own semantics thanks to this particular function, although other string functions will find decent work.

Nothing is simpler: explode PHP simply splits one line at the places where another line appears in it. This is usually a single character:

  • comma ",";
  • point ".";
  • slash "/";
  • tilde "~";
  • and anything to the taste of the developer.

But you can break the line for anything, and the second line (delimiter) can make sense of the whole tag, tag element or carry any value within the imagination of the programmer.

Case when a number is a string

Numbers tend to participate in mathematical operations, and strings in lowercase. The idea is correct and interesting, but old and useless.

Today, a variable can change its type along the way from one operator to another, and in each syntactic construct, all the variables that participate in it have the type that defines the syntax of this construct.

So if a number falls into a string construction, it becomes a string, and if a string participates in a mathematical operation, then all its beginning before the first occurrence of a non-digital character will be interpreted as a number.

Author's example is not a picture

In this code, explode in PHP demonstrates how you can easily separate the integer part from the fractional part for any number of digits. The result of executing the code below is presented.

Author's example is not a picture

It is doubtful that such a meaning was put into this function by the creators of the language. But there are a great many variants of such an application for this function.

It should be borne in mind that delimiter (the line by which the break occurs) disappears, that is, after the explode line is split, PHP extracts all the delimiter found from it.

Case when a string is an array

In the above example, the string of digits is broken by explode PHP by comma, and an array is created. PHP inverse function implode can merge this array into a single line, for example, through a dot.

explode in php

In the first case, the string does not change; in the second case, the array also remains. In other words, using strings, you can split on one base, and connect in a different way.

In a first approximation, for example, you can replace lowercase letters with uppercase. Such a task for explode / implode, probably, will not be very interesting, but the moment is important that the partition can be any! How to connect is also any option.

Lines and objects

Object-oriented programming is everywhere today, and any program consists of objects. Naturally, the PHP explode description never mentioned objects, but there is nothing stopping the idea of ​​a direct connection between strings and objects.

In this context, an object can be represented by a string of characters, placing it in a database or transmitting it over communication channels. It is possible to organize the exchange of information between objects not through bulky arrays, but through parameter lines.

php explode description

The sender combines the necessary data into a single whole (string), and the receiver using explode PHP restores them, that is, extracts the desired sequence of characters.

If you develop this idea at the time when the object is created, then you usually have to "manually" or from the database fill it with the necessary data.

Using explode, you can automate the process of creating instances of an object, and having an abstract object, you can control its type by transmitting various character strings for initialization. This approach reduces the number of different objects and simplifies their manipulation.


All Articles