Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithmic complexity of PHP function strlen()

Recently I was asked this question on interview and I didn't know how to answer it.

Can anyone answer this question and describe it?

like image 448
Chvanikoff Avatar asked Mar 13 '11 20:03

Chvanikoff


People also ask

What does strlen () function do in PHP?

The strlen() function returns the length of a string.

What is the use of strlen () function in PHP Mcq?

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.

How do I count characters in PHP?

The count_chars() function returns information about characters used in a string (for example, how many times an ASCII character occurs in a string, or which characters that have been used or not been used in a string).


1 Answers

O(1) since the length is stored as an attribute: source

However, this trivia is worth countering with a discussion about micro-optimising theatre, as kindly provided by our hosts here and here; read those two links and you'll find a good talking point to change the momentum of the conversation next time similar questions come up, regardless of whether you know the particular answer!

How the interviewer reacts to your tangent will tell you a lot about how much you want to work with them..

like image 102
Will Avatar answered Sep 22 '22 02:09

Will