Beginner PHP Web Programmers: String Length

Any self-respecting web programmer should know the PHP language that can be used to create web pages. This language is based on C and C ++, and therefore there are many constructs characteristic of this language. For example, in PHP, the string length is returned by a function with the same name as in C. This article talks about strings, as well as the operations that can be performed on them.

php string length
But before talking about functions, we give a basic definition. A string is a sequence of characters, each of which occupies exactly one byte. So, PHP supports 256 characters. It logically follows that there is no Unicode support in this language - there are much more characters in Unicode.

What string operations are there in PHP? The length of a string, their concatenation (connection), return of a character code, and vice versa are the simplest examples. There is a more complicated one - substring search, hashing, inversion. And there are such as removing tags, searching by template, breaking into substrings using a separator ... You won’t list everything! Therefore, we will talk about the most common and useful of them.

The very first and most commonly used function in PHP is the length of the string. It is called strlen, and as a parameter takes a single sequence of characters. Everything is simple.

<?

$ foo = ”foo”;

$ bar = strlen ($ foo); // $ bar is three

?>

In PHP, the string length can be zero. Such a line is called empty.

The next function is concatenation or merging. Simply put, she makes one of two lines.

<?

$ foo = ”Hello,”;

$ bar = "world!";

$ baz = concat ($ foo, $ bar);

echo $ baz; // print “Hello world!”

?>

php string length
There are two inverse functions for working with individual characters. Ord - returns the character code, and chr - defines the character by code.

<?

$ foo = ”q”;

$ bar = ord ($ foo); // $ bar is 113

$ baz = chr ($ bar); // $ baz is “q”

?>

We cannot but mention another interesting function - date. In PHP, strings can be used as a template for time output. For example, if you transfer the string “H: m: s” to date and display the result, the current server time will appear on the screen, separated by a colon, for example, “11:08:34”.

Now consider the explode function. It breaks the string into elements, considering the boundary of each specified character. It sounds rather complicated, but in fact, everything is clear enough. For example, there is the line “root | 12: 56: 49 | wheel”. Then you can write like this:

$ foo = explode (“root | 12: 56: 49 | wheel”, “|”);

As a result, the $ foo array will contain three elements - “root”, “12:56:49”, “wheel”. If you do not specify a separator when calling the function, then by default it will be considered a space. There is another optional parameter - the maximum number of allocated substrings.

php string
The following function removes HTML tags from a string. It is called strip_tags. Like the explode function, it has an optional second parameter - a list of tags that must be left.

Simple functions - concatenation, substring search, string length - PHP interprets quickly enough. But processing a search by template or removing tags can take quite a while. Therefore, programs need to be compiled competently, taking into account the fact that its implementation takes some time. A normal script should run for a maximum of 10 seconds, and after that you need to display at least part of the web page, otherwise the user will leave it. But usually web applications run much faster, because the PHP code is executed on the server, and the browser accepts a page based on the script that contains the output of the PHP script.

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


All Articles