Question about 'sizeof'

This is a discussion on Question about 'sizeof' within the PHP Language forums, part of the PHP Programming Forums category; Hello, I have another simple question about an array in PHP and a variable in PHP. This is the array: $...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default Question about 'sizeof'

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

Reply With Quote
  #2 (permalink)  
Old 5 Days Ago
Piotr
 
Posts: n/a
Default Re: Question about 'sizeof'

SM 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?


You should use variable variable:
$item_total = sizeof($$item);

Mark double $$
This is true in any situation, not only with sizeof

best regards
Piotr N
Reply With Quote
  #3 (permalink)  
Old 5 Days Ago
ELINTPimp
 
Posts: n/a
Default Re: Question about 'sizeof'

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
Reply With Quote
  #4 (permalink)  
Old 5 Days Ago
ELINTPimp
 
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


Ahhh, now i understand after reading Pitor's reply. Yes, he is
correct, I read the question differently.

Steve
Reply With Quote
  #5 (permalink)  
Old 5 Days Ago
Piotr
 
Posts: n/a
Default Re: Question about 'sizeof'

>> $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?

>
> You should use variable variable:
> $item_total = sizeof($$item);
>
> Mark double $$
> This is true in any situation, not only with sizeof


Forgot about one small detail:
$item = 'thumbs_cat_' . $thumb; //this outputs a string:
---------^ loose the '$';

best regards
Piotr N
Reply With Quote
  #6 (permalink)  
Old 5 Days Ago
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
  #7 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default Re: Question about 'sizeof'

On May 8, 11:42 am, Piotr <s...@poczta.onet.pl> wrote:
> >> $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?

>
> > You should use variable variable:
> > $item_total = sizeof($$item);

>
> > Mark double $$
> > This is true in any situation, not only with sizeof

>
> Forgot about one small detail:
> $item = 'thumbs_cat_' . $thumb; //this outputs a string:
> ---------^ loose the '$';
>
> best regards
> Piotr N


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
  #8 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default Re: Question about 'sizeof'

On May 8, 11:42 am, Piotr <s...@poczta.onet.pl> wrote:
> >> $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?

>
> > You should use variable variable:
> > $item_total = sizeof($$item);

>
> > Mark double $$
> > This is true in any situation, not only with sizeof

>
> Forgot about one small detail:
> $item = 'thumbs_cat_' . $thumb; //this outputs a string:
> ---------^ loose the '$';
>
> best regards
> Piotr N


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
  #9 (permalink)  
Old 5 Days Ago
SM
 
Posts: n/a
Default Re: Question about 'sizeof'

On May 8, 11:50 am, SM <servandomont...@gmail.com> wrote:
> On May 8, 11:42 am, Piotr <s...@poczta.onet.pl> wrote:
>
>
>
> > >> $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?

>
> > > You should use variable variable:
> > > $item_total = sizeof($$item);

>
> > > Mark double $$
> > > This is true in any situation, not only with sizeof

>
> > Forgot about one small detail:
> > $item = 'thumbs_cat_' . $thumb; //this outputs a string:
> > ---------^ loose the '$';

>
> > best regards
> > Piotr N

>
> 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


Maybe i should open a new post for this one, but here it goes:

I'm stock! When i want to reference that variable in a echo statment,
it does't work. Any suggestions?
Thanks

<li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
$category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
alt="" /></a></li>
Reply With Quote
  #10 (permalink)  
Old 5 Days Ago
Piotr
 
Posts: n/a
Default Re: Question about 'sizeof'

SM wrote:
> On May 8, 11:50 am, SM <servandomont...@gmail.com> wrote:
>> On May 8, 11:42 am, Piotr <s...@poczta.onet.pl> wrote:
>>
>>
>>
>>>>> $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?
>>>> You should use variable variable:
>>>> $item_total = sizeof($$item);
>>>> Mark double $$
>>>> This is true in any situation, not only with sizeof
>>> Forgot about one small detail:
>>> $item = 'thumbs_cat_' . $thumb; //this outputs a string:
>>> ---------^ loose the '$';
>>> best regards
>>> Piotr N

>> 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

>
> Maybe i should open a new post for this one, but here it goes:
>
> I'm stock! When i want to reference that variable in a echo statment,
> it does't work. Any suggestions?
> Thanks
>
> <li><a href="<?php echo $SERVER['PHP_SELF']; ?>?cat=<?php echo
> $category_this; ?>"><img src="images/<?php echo($$item[$i]);?>"
> alt="" /></a></li>


:)
It should look like this. Mark the { and }
echo ${$item[$i]};

As a side note, is this really best way to access needed data this way ?
Cant you keep original values in the array instead of the variable names ?

best regards
Piotr N
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 09:52 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0