Home > Article > Backend Development > Analysis of confusing problems in length calculation in PHP, PHP length calculation confusion_PHP tutorial
The example of this article tells about the confusion about length calculation in PHP. Share it with everyone for your reference, the details are as follows:
I am often confused by the character functions of arrays and strings in PHP. Here is a summary:
strlen($string) function: calculate the length of a 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 cells in var, usually an array, any other type has only one cell.
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 a multidimensional array. The default value for mode is 0. count() does not recognize infinite recursion.
In short, use strlen() for strings and sizeof() for arrays.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP office document operation skills (including word, excel, access, ppt)", "Complete of PHP array (Array) operation skills", " PHP Sorting Algorithm Summary", "PHP Common Traversal Algorithms and Techniques Summary", PHP Data Structure and Algorithm Tutorials, PHP Programming Algorithm Summary, PHP Mathematical Operation Techniques Summary, PHP Regular Expression Usage Summary, "Summary of PHP operations and operator usage", "Summary of PHP string usage" and "Summary of common PHP database operation skills"
I hope this article will be helpful to everyone in PHP programming.