Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - why doesn't count() work like strlen() on a string?

Tags:

php

A string is an array of characters, correct? If so, then why does count() not produce the same result as strlen() on a string?

like image 706
Major Productions Avatar asked Jun 24 '11 11:06

Major Productions


People also ask

How do I count characters in a string in PHP?

The strlen() is a built-in function in PHP which returns the length of a given string. It takes a string as a parameter and returns its length. It calculates the length of the string including all the whitespaces and special characters.

Can we use strlen with string?

Threadsafe: Yes. The strlen() function determines the length of string excluding the ending null character. The strlen() function returns the length of string . This example determines the length of the string that is passed to main() .

Does strlen count null?

The strlen() function calculates the length of a given string. The strlen() function is defined in string. h header file. It doesn't count null character '\0'.


1 Answers

Unless it is an object, count casts its argument to an array, and

  count((array) "example")
= count(array("example"))
= 1
like image 113
phihag Avatar answered Oct 14 '22 23:10

phihag