Partial macro expansion of variable and function name.

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

    Partial macro expansion of variable and function name.

    I come from the Visual Foxpro world, which is one reason I love PHP.
    VFP is a scripting type language with macro substitution abilities
    similar to PHP.

    Besides the regular expansion I can do crazy things (VFP uses & instead
    of $):

    x="sales"
    sales="1000"
    salestax="8.25"
    ? &x
    ..........print s 1000
    ? &x.tax
    ............pri nts 8.25

    tr="trim"
    ? &tr.("xxx ")
    ..........print s xxx

    ? 'This function is &tr.("xxxxx ")'
    ....... prints This function is trim("xxxxx ")

    In VFP the & indicates macro expansions and a space or a . idnicates
    the variable termination. Can I do this kind of partial expansion in
    PHP? There must be a trick somewhere.

    Thanks.

  • tihu

    #2
    Re: Partial macro expansion of variable and function name.


    ImOk wrote:[color=blue]
    > I come from the Visual Foxpro world, which is one reason I love PHP.
    > VFP is a scripting type language with macro substitution abilities
    > similar to PHP.
    >
    > Besides the regular expansion I can do crazy things (VFP uses & instead
    > of $):
    >
    > x="sales"
    > sales="1000"
    > salestax="8.25"
    > ? &x
    > .........prints 1000
    > ? &x.tax
    > ...........prin ts 8.25
    >
    > tr="trim"
    > ? &tr.("xxx ")
    > .........prints xxx
    >
    > ? 'This function is &tr.("xxxxx ")'
    > ...... prints This function is trim("xxxxx ")
    >
    > In VFP the & indicates macro expansions and a space or a . idnicates
    > the variable termination. Can I do this kind of partial expansion in
    > PHP? There must be a trick somewhere.
    >
    > Thanks.[/color]

    Variable variables might be what you are looking for, they use $$
    instead of $



    Basically

    $name = 'joe';
    $getvar = 'name'
    echo $$getvar; //prints $name so the output is 'joe'

    It works with functions and class methods too

    $tr = 'trim'
    echo $tr(' xxxxx '); // calls the function trim() and prints
    xxxx

    Seeya

    Tim

    Comment

    • tihu

      #3
      Re: Partial macro expansion of variable and function name.

      ImOk wrote:[color=blue]
      > I come from the Visual Foxpro world, which is one reason I love PHP.
      > VFP is a scripting type language with macro substitution abilities
      > similar to PHP.
      >
      > Besides the regular expansion I can do crazy things (VFP uses & instead
      > of $):
      >
      > x="sales"
      > sales="1000"
      > salestax="8.25"
      > ? &x
      > .........prints 1000
      > ? &x.tax
      > ...........prin ts 8.25[/color]

      Just noticed the &$.tax thingy, neat trick..

      In php you need to use the dot operator( joins two strings together)
      and curly braces ( Think of it like php interprets the contents of {}
      as an expression)


      $x = 'sales'
      $sales = 1000;
      $salestax = 8.25;

      echo $$x; //prints 1000
      echo ${$x.'tax'}; //prints 8.25

      but
      echo $$x.'tax'; // without {} it prints 1000tax

      Tim

      Comment

      • ImOk

        #4
        Re: Partial macro expansion of variable and function name.

        Aha, the old {} trick. But how does it work with a function name?

        E.g.
        $trm="trim";

        echo ${'l' . $trm}(" xxxxxx"); // should execute ltrim("
        xxxxxx");
        echo ${'r' . $trm}(" xxxxxx");

        The above don't work. What am I doing wrong?

        Comment

        • tihu

          #5
          Re: Partial macro expansion of variable and function name.


          ImOk wrote:[color=blue]
          > Aha, the old {} trick. But how does it work with a function name?
          >
          > E.g.
          > $trm="trim";
          >
          > echo ${'l' . $trm}(" xxxxxx"); // should execute ltrim("
          > xxxxxx");
          > echo ${'r' . $trm}(" xxxxxx");
          >
          > The above don't work. What am I doing wrong?[/color]

          You're doing nothing wrong. If you'd asked an hour ago I would have
          said thats the right way to do it so I'm as surprised as you are

          I get errors saying the variable named ltrim doesnt exist, then another
          error saying the function name should be a string. (Assume the function
          name is seen as being null because the variable ltrim doesnt exist).
          Maybe the precedence of () is lower than ${}, not really sure...

          The next best thing php has is call_user_func. .

          echo call_user_func( 'l'.$trm , "xxxx" );

          I did kinda manage to get a workaround but its not nice. Php is looking
          for a variable name ltrim (instead of calling the function ltrim) so
          give it what it wants and set the value of $ltrim to 'ltrim'
          dynamically. Surprisingly it works..

          $trm='trim';

          echo ${ ${'l'.$trm} = 'l'.$trm }(' xxxx '); //ewww

          Best use call_user_func. ..

          Seeya

          Tim

          Comment

          Working...