size of a variable

Busy's picture

He has: 6,151 posts

Joined: May 2001

why wont this work?

if (sizeof($item) <= 1) {
echo "you are too small";

} elseif (sizeof($item) >= 14) {
echo "you are too big";

} else {
echo ("you are just right");
}

I also tired count() and set sizeof($item) to a variable first and put the variable against the < and >

The "$item" is a search word from a form, the form works great but the above will only ever output "you are to small"

[edit] If I do $the_length = sizeof($item); echo $the_length; the output is 1
I think it was the manual says sizeof is for variables and arrays but input from users say it's only for arrays (I'm using a variable)[/edit]

He has: 1,016 posts

Joined: May 2002

<?php
if(strlen($item) <= 1) {
}
?>

More info: http://www.php.net/strlen

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Quote: The sizeof() function is an alias for count().

It simply returns the number of elements in an arrray.

Busy's picture

He has: 6,151 posts

Joined: May 2001

Thanks, strlen does the trick.

Mark, yeah thats what I saw in the manual but found notes around the place said it could be used for counting a variables content, so much bad and confusing stuff on the web

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.