Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    478
    Thanks
    3
    Thanked 0 Times in 0 Posts

    mmm how do i describe this? i have a string within a string that does not work.

    i have all my variables in a vars.php file and this is included at the top of al my pages.

    varsfile.php

    Code:
    <?php
    $emailtextstring = "Thank you $namestring for signing up to xxxxx";
    //   $namestring does not yet have a value
    ?>
    in my other pages i wish to say use this variable $emailtextstring as its full contents would be html and have a lot more in it but this is just an example to show what i have problems with.

    i wish to use this to email the user welcoming them.

    how do i have a string use a variable later on in a script before it is setup?



    edited...
    i have just tried this out and seem to get the string name to show but not the content
    Code:
    <?
    
    // yes this line first!
    $test = "\$name";
    
    
    // yes this line second!
    $name = "jason";
    
    
    echo($test);
    // output should be...
    
    //  jason
    
    
    // but it outputs....
    
    //  $name
    ?>
    Last edited by jasonc310771; 08-21-2009 at 09:43 PM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by jasonc310771 View Post
    i have all my variables in a vars.php file and this is included at the top of al my pages.

    varsfile.php

    Code:
    <?php
    $emailtextstring = "Thank you $namestring for signing up to xxxxx";
    //   $namestring does not yet have a value
    ?>
    in my other pages i wish to say use this variable $emailtextstring as its full contents would be html and have a lot more in it but this is just an example to show what i have problems with.

    i wish to use this to email the user welcoming them.

    how do i have a string use a variable later on in a script before it is setup?



    edited...
    i have just tried this out and seem to get the string name to show but not the content
    Code:
    <?
    
    // yes this line first!
    $test = "\$name";
    
    
    // yes this line second!
    $name = "jason";
    
    
    echo($test);
    // output should be...
    
    //  jason
    
    
    // but it outputs....
    
    //  $name
    ?>
    The easiest solution is to use referenced values. Even during assignment, if a non-existant reference is used, it will create it in the current scope without a warning. When a variable under the same name is later found it will use the already defined variable under that name:
    PHP Code:
    $test = &$name;
    $name 'Jason';
    printf("\$test = %s\n"$test); 
    Another solution is to put the PHP back together using eval().

    Edit:
    I should mention, that in either case you could be walking into trouble. Should a referenced or eval'd variable be used and not explicitly defined, if register_globals are in use you've now created a gigantic security hole since users can enter their own variables for the replacement code. On the plus side, the client needs to know what the variables in use are, and if you've disabled error reporting than there shouldn't be any issues.
    Last edited by Fou-Lu; 08-21-2009 at 09:56 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

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