...

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



jasonc310771
08-21-2009, 09:36 PM
i have all my variables in a vars.php file and this is included at the top of al my pages.

varsfile.php



<?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


<?

// yes this line first!
$test = "\$name";


// yes this line second!
$name = "jason";


echo($test);
// output should be...

// jason


// but it outputs....

// $name
?>

Fou-Lu
08-21-2009, 09:54 PM
i have all my variables in a vars.php file and this is included at the top of al my pages.

varsfile.php



<?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


<?

// 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:


$test = &$name;
$name = 'Jason';
printf("\$test = %s\n", $test);

Another solution is to put the PHP back together using eval().


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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum