View Single Post

  #6 (permalink)  
Old 05-08-2008
SM
 
Posts: n/a
Default Re: Question about 'sizeof'

On May 8, 11:36 am, ELINTPimp <smsi...@gmail.com> wrote:
> On May 8, 11:15 am, SM <servandomont...@gmail.com> wrote:
>
>
>
> > Hello,
> > I have another simple question about an array in PHP and a variable in
> > PHP.

>
> > This is the array:

>
> > $thumbs_cat_1 = array(
> > 'wine',
> > 'cheese',
> > 'ice',
> > 'bread'
> > );

>
> > $thumbs_cat_2 = array(
> > 'hello',
> > 'goodbye'
> > );

>
> > This will work:
> > $item_total = sizeof($thumbs_cat_1);
> > output: 4

>
> > $item_total = sizeof($thumbs_cat_2);
> > output: 2

>
> > Now, i want to replace the parameter in 'sizeof' by a variable

>
> > $thumb = 1;
> > $item = '$thumbs_cat_' . $thumb; //this outputs a string:
> > $thumbs_cat_1

>
> > $item_total = sizeof($item);
> > output: Nothing!

>
> > How do i pass a variable to the 'sizeof' function?

>
> > Thanks
> > Marco

>
> Marco,
>
> You should be getting a return of 1 (integer) for a string passed to
> the count() function. (sizeof is an alias of count)
>
> To get the length of a string, you would traditionally use
> strlen($item), which would return the length of a string.
>
> Regards,
>
> Steve


I don't need the length of the string , just the actual variable. But
i did like the fact that sizeof is an alias of count. So i will use
count instead.
I didn't know about the $$ tip. Wow! so easy. Thanks, It Works!

Heres the final code:

$thumb = 1;
$item = '$thumbs_cat_' . $thumb;
$item_total = count($item);

Thanks again
Marco
Reply With Quote