ComputersProgramming

Beginners PHP programmers: length of the string

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

But before we talk about functions, we give the basic definition. A string is a sequence of characters, each of which occupies exactly one byte. This means that PHP supports 256 characters. From this it follows logically that there is no support for Unicode in this language - in fact there are much more characters in Unicode.

What operations with lines are in PHP? The length of the string, their concatenation (connection), the return of the symbol code and vice versa are the simplest examples. There is even more difficult - searching for substrings, hashing, inverting. And there are also such as deleting tags, searching by pattern, breaking down into substrings using a delimiter ... You can not enumerate everything! Therefore, we will talk about the most common and useful ones.

The very first and often used function in PHP is the length of the string. It is called strlen, and takes as one parameter a single sequence of characters. It's simple.

$ Foo = "foo";

$ Bar = strlen ($ foo); // $ bar is equal to three

?>

In PHP, the length of the string can be zero. This line is called empty.

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

$ Foo = "Hello,";

$ Bar = "world!";

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

Echo $ baz; // output "Hello world!"

?>

There are two reverse functions for working with individual symbols. Ord - returns the character code, and chr - specifies the character by code.

$ Foo = "q";

$ Bar = ord ($ foo); // $ bar is equal to 113

$ Baz = chr ($ bar); // $ baz is equal to "q"

?>

It is impossible not to mention one more interesting function - date. In PHP, strings can be used as a template for outputting time. For example, if you pass the string "H: m: s" to date and display the result, then the current server time, separated by a colon, will appear on the screen, for example, "11:08:34".

Now consider the function explode. It breaks the string into elements, counting each symbol as the border of each. It sounds rather difficult, but in fact everything is quite clear. 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 array $ foo will contain three elements - "root", "12:56:49", "wheel". If you do not specify a delimiter when calling the function, then by default it will be considered a space. There is one more optional parameter - the maximum number of allocated substrings.

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

Simple functions - concatenation, substring search, string length - PHP interprets fairly quickly. But the processing of the search by template or deletion of tags can be done long enough. Therefore, programs need to be written competently, taking into account the fact that it takes a certain amount of time. The normal script should be executed 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 work much faster, because PHP code runs on the server, and the browser accepts a script-generated page that contains the output of the PHP script.

Similar articles

 

 

 

 

Trending Now

 

 

 

 

Newest

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