Need education: Question about indirect referencing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lwoods

    Need education: Question about indirect referencing

    Example:

    $x=array('a'=>' ant','b'=>'boat ');
    $s='a';
    $y='$s';
    echo $x[$$y];

    I want to echo the value of key 'a' by indirectly referencing it via the
    variable $y. The above is NOT correct, I know.

    How do I indirectly reference a variable in PHP?

    TIA,

    Larry Woods


  • Darvin

    #2
    Re: Need education: Question about indirect referencing

    lwoods wrote:[color=blue]
    > Example:
    >
    > $x=array('a'=>' ant','b'=>'boat ');
    > $s='a';
    > $y='$s';
    > echo $x[$$y];
    >
    > I want to echo the value of key 'a' by indirectly referencing it via the
    > variable $y. The above is NOT correct, I know.
    >
    > How do I indirectly reference a variable in PHP?
    >
    > TIA,
    >
    > Larry Woods
    >
    >[/color]
    $y='s';
    fix your example, but I don't understand why you can't use 'echo $x[$s];' .

    Darvin

    Comment

    • Janwillem Borleffs

      #3
      Re: Need education: Question about indirect referencing

      lwoods wrote:[color=blue]
      > I want to echo the value of key 'a' by indirectly referencing it via
      > the variable $y. The above is NOT correct, I know.
      >
      > How do I indirectly reference a variable in PHP?
      >[/color]

      The technique is called 'Variable variables' and looks like this:

      $x=array('a'=>' ant','b'=>'boat ');
      $s='a';
      $y='s'; // Removed the dollar sign
      echo $x[$$y];


      JW


      Comment

      • lwoods

        #4
        Re: Need education: Question about indirect referencing

        That's the structure that I'm looking for.

        Thanks.

        Larry

        "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
        news:43ca4a97$0 $79824$dbd45001 @news.euronet.n l...[color=blue]
        > lwoods wrote:[color=green]
        >> I want to echo the value of key 'a' by indirectly referencing it via
        >> the variable $y. The above is NOT correct, I know.
        >>
        >> How do I indirectly reference a variable in PHP?
        >>[/color]
        >
        > The technique is called 'Variable variables' and looks like this:
        >
        > $x=array('a'=>' ant','b'=>'boat ');
        > $s='a';
        > $y='s'; // Removed the dollar sign
        > echo $x[$$y];
        >
        >
        > JW
        >
        >[/color]


        Comment

        Working...