SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Easy syntax question re: the 'list' function

    This tiny problem is bugging the hell out of me. I'm sure the solution is staring me in the face. I'm trying to dynamically provide variables for the list function...

    <?

    // This works
    $string="1,3,5";
    list($a,$b,$c) = explode(",",$string);
    echo "$a,$b,$c"; // outputs 1,3,5

    // This doesn't work, but demonstrates what I'm trying to achieve.
    $string="1,3,5";
    $list="$d,$e,$f";
    list($list) = explode(",",$string);
    echo "<p>$d,$e,$f,$list"; // outputs ,,,1

    ?>

    Of course, $list is not 'expanding' to $d,$e,$f in the list function. The original contents of $list is being overwritten by the first element of the comma-delimited $string (the number 1). It must be possible to make it do what I want using another function? Can someone put me out of my misery please :-)
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $list="$d,$e,$f";
    PHP will parse the contents of a string inclosed by double quotes and substitute the values of the variables it finds in there. At this point in your code neither $d, $e or $f exist, so PHP will declare them and they will not have been initialised with any variables. So at the end of that statement you will have assigned the string ",," to $list (ie, two commas).
    list($list) = explode(",",$string);
    Now you are exploding the string $string into an array using a comma as the delimiter. And you are assigning the elements of this array to a list of variables defined in list($list). But wait! You only have one variable in your list, the string $list -> so only the first element of the array returned by explode() is going to get assigned to it.
    echo "<p>$d,$e,$f,$list";
    Finally you echo $d (which is empty), a comma, $e (empty), a comma, $f (empty), a comma and $list holds the value 1.

    I'm affraid I don't quite get what it is you are trying to do with that code. Could you please explain your requirements a little more?

  3. #3
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You know what? I think what you want to do is use variable variables. http://www.php.net/manual/en/languag...s.variable.php I've never tried using them. They are a little spaced-out for me to get my head around. However, I see that you are wearing appropriate protection so you might want to give them a try. I'll come back and edit this post and add some code if I think I can come up with how they might apply to your case.

  4. #4
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi freakysid, and thanks for your reply.

    At the risk of sounding ungrateful (I'm not, honest!), I know exactly why the second example doesn't work, and why it does what it does :-) I was just hoping that by using it, it would help to describe what I'm trying to achieve - the dynamic assignment of variables inside the list() function.

    Another example:
    $string="1,3,5";
    list($a,$b,$c) = explode(",",$string);
    echo "<p>$a,$b,$c"; // outputs 1,3,5
    list($x,$y,$z) = explode(",",$string);
    echo "<p>$x,$y,$z"; // outputs 1,3,5

    In other words, I want to dynamically determine which 3 variable names are going to be assigned values from the exploding of $string. Maybe I want $a,$b,$c to pick up the values, or maybe I want $x,$y,$z to pick up the values. This is what I was trying to explain in my initial posting using the $list example.

    I have used variable variables b4. If my understanding of their usage is correct, then they'd not be any use in this case. I'm thinking maybe the solution has something to do with the eval() function or similar...

    I should point out that this is just one of those annoying little programming issues that (in the application I'm using it) I can easily code around, so I'm just trying to indulge my perfectionist tendencies.
    Last edited by spaceman; Jun 16, 2001 at 05:21.
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No worries - you've lost me I'm affraid. I look forward to hearing what the solution is (although I'm not sure of the problem)

  6. #6
    Don't eat yellow snow spaceman's Avatar
    Join Date
    Mar 2001
    Location
    Melbourne, Australia
    Posts
    1,039
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe I'm just a sad person who should get a life on a Saturday night. Luckily I have a sympathetic fiance (especially since it's her web site I'm slaving - oops! - working on)
    Web Design Perth Melbourne .:. Itomic Business Website Solutions
    Drupal Experts .:. Drupalise


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •