Jump to content

Array .length property weirdness


IAmBroom
 Share

Recommended Posts

I've stripped my code down to the bare basics, and it still is confusing me:

<?php	$str[] = "Bob";	echo $str.length;?>

I expect an output of

1

but instead I get

Arraylength

WTF?Update: The non-standard function sizeof() works. This code:

<?php	$str[] = "Bob";	echo $str.length . "<br />";	echo sizeof($str) . "<br />";   var_dump($str) . "<br />";?>

results in this output:

Arraylength1array(1) { [0]=> string(3) "Bob" }

Edited by IAmBroom
Link to comment
Share on other sites

Dot is concatenatio operator in php unlike js where it is used to access objecj property and method|Array convert into string when you conacatenat and prints 'array' and 'length' tries to find constant of same name if its not there it assumes string and convert to it. Though it throw some error. You should set your php to show all type of error which are helpful for debugThere is count() to count array element

  • Like 1
Link to comment
Share on other sites

Sorry, for the inconvenience, It is hard to type from mobile that much of text. It looks like the text has been messed up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...