Home>Article>Backend Development> A simple analysis of confusing issues regarding length calculation in PHP

A simple analysis of confusing issues regarding length calculation in PHP

墨辰丷
墨辰丷 Original
2018-06-02 09:53:41 1711browse

This article mainly introduces the confusion about length calculation in PHP, and briefly analyzes PHP’s relevant precautions regarding array and string length operations. Friends in need can refer to the following

often used by PHP I am confused by the character functions of arrays and strings. Here is a summary:

strlen($string) function: Calculate the length of the string;

sizeof ($string,$mode) function: It is an alias of the count() function. The count() function is as follows:

int count (mixed var [, int mode] )

Returns the number of units in var, usually an array, any other type has only one unit.

For objects, if SPL is installed, count() can be called by implementing the Countable interface. This interface has only one method, count(), which returns the return value of the count() function.

If var is not an array type or an object that implements the Countable interface, 1 will be returned, with one exception, if var is NULL the result is 0.

Note:The optional mode parameter is available since PHP 4.2.0.

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will count the array recursively. Particularly useful for computing all elements of multidimensional arrays. The default value for mode is 0. count() does not recognize infinite recursion.

In short, use strlen() for strings and sizeof() for arrays.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpExample sharing of processing single file and multiple file uploads

PHP implementation between pages Methods of passing values and maintaining values

Methods to implement scheduled execution functions based on the sleep function in PHP

The above is the detailed content of A simple analysis of confusing issues regarding length calculation in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn