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: $...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
>> $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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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> |
|
|||
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
|
|