Bluehost.com Web Hosting $6.95

Variable question

This is a discussion on Variable question within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hey, I'm trying to define a variable that has a variable in it, can this be done and what ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-02-2004
Carlos Ramirez
 
Posts: n/a
Default Variable question

Hey,

I'm trying to define a variable that has a variable in it, can this be
done and what would be the syntax.

I read the PHP manual section on variable variables but this is not what
I'm looking for.

This is what I'm trying to do:

I'm loading values into an array and I need to define various different
name arrays that have a common part in the name.

$myarray1$variable[] = ( x value );
$myarray2$variable[] = ( x value );

Thanks,

Carlos

Reply With Quote
  #2 (permalink)  
Old 03-02-2004
Mike Peters
 
Posts: n/a
Default Re: Variable question

On 2004-03-02, Carlos Ramirez wrote:
> Hey,
>
> I'm trying to define a variable that has a variable in it, can this be
> done and what would be the syntax.
>
> I read the PHP manual section on variable variables but this is not what
> I'm looking for.
>
> This is what I'm trying to do:
>
> I'm loading values into an array and I need to define various different
> name arrays that have a common part in the name.
>
> $myarray1$variable[] = ( x value );
> $myarray2$variable[] = ( x value );
>
> Thanks,
>
> Carlos
>


Does this help?

<?php
$myarray1 = "array_one";
$myarray2 = "array_two";

$variable = "_var";

${$myarray1.$variable} = array(1, 2, 3);
${$myarray2.$variable} = array('a', 'b', 'c');
$this_var1 = $myarray1.$variable;
$this_var2 = $myarray2.$variable;

print_r($$this_var1);
print_r($$this_var2);
print_r($array_one_var);
print_r($array_two_var);
?>

--
Mike Peters
mike [-AT-] ice2o [-DOT-] com
http://www.ice2o.com
Reply With Quote
  #3 (permalink)  
Old 03-03-2004
Nelson Menezes
 
Posts: n/a
Default Re: Variable question

Carlos Ramirez wrote:

> Hey,
>
> I'm trying to define a variable that has a variable in it, can this be
> done and what would be the syntax.
>
> I read the PHP manual section on variable variables but this is not what
> I'm looking for.
>
> This is what I'm trying to do:
>
> I'm loading values into an array and I need to define various different
> name arrays that have a common part in the name.
>
> $myarray1$variable[] = ( x value );
> $myarray2$variable[] = ( x value );
>
> Thanks,
>
> Carlos


$prefix = '$myarray';
$suffix = $variable;
$code = $prefix . $suffix . "[] = ($value);"; // don't forget that ';'
eval($code);

Check here, this is good:
http://www.php.net/manual/en/function.eval.php

--
Nelson Menezes
http://burninghorizons.net
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

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

BB 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 06:16 PM.


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