Mombu the Php Forum

Go Back   Mombu the Php Forum > Php > variable substitution
User Name
Password
REGISTER NOW! Mark Forums Read




Reply
1 4th November 10:06
jekillen
External User
 
Posts: 1
Default variable substitution



Hello again;
I have two variables declared in the global scope of a script.
$string_a = 'stuff $string_b and more stuff';
$string_b = '';
One is a string with a reference for substitution to the other
string which is empty.
In the processing body of the script are if/if else blocks.
In these blocks I want to use $string_a and
set $string_b to a value
if( condition)
{ $string_b = 'by the way;';... etc
so $string_a should read:
"stuff and by the way; and more stuff"
But this substitution will not take place
in the context of the else if block. I do not
want to write $string_a in at least 5 different
if else blocks because it is about 10 lines
intended to be an e-mail message body -> !SPAM.

this script is used to process data sent from a
link in another e-mail message used to validate
and e-mail address.

Q: Is there a way to get the substitution to take
place here? (by reference, maybe?)

Thank you in advance for info
Jeff K
  Reply With Quote


 


2 4th November 10:06
heavyccasey
External User
 
Posts: 1
Default variable substitution



Hmmm... will this work?

function get_string_a() {
global $string_b;
return "stuff $string_b and more stuff";
}

-Casey
  Reply With Quote
3 4th November 10:07
ceo
External User
 
Posts: 1
Default variable substitution


http://php.net/str_replace

--
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/from/lynch
Yeah, I get a buck. So?
  Reply With Quote


 


4 5th November 02:20
ausmusj
External User
 
Posts: 1
Default variable substitution


Several ways to do this (and avoid globals or a str_replace call):

#1:

if (condition)
{
$string_b = 'blah';
} else if (condition2)
{
$string_b = 'foo';
} else if (condition3)
{
$string_b = 'bar';
} else
{
$string_b = 'other';
}

$string_a = "stuff $string_b and more stuff";

The reason for this is the variable substition occurs *at the time of
assignment* - not later.

Another way, if it's not easy to have your string_b setting
conditionals all in a row like that (or if this needs to be done
elsewhere in code, as well), would be:

function assignStrA($subStr)
{
$retVal = "stuff $subStr and more stuff";
return $retVal;
}

if (cond1)
{
$string_a = assignStrA('foo');
} else if (cond2)
{
$string_a = assignStrA('bar');
} else
{
$string_a = assignStrA('other');
}


HTH-

James Ausmus
Integration Software Engineer
HTRI
"Cross Technology Integration Specialists" 503-538-8085
  Reply With Quote
5 5th November 02:21
jekillen
External User
 
Posts: 1
Default variable substitution


O.K. that looks good, I can put in a target string to replace
instead of the variable.
Thanks for the suggestion;
jeff K
  Reply With Quote
6 5th November 02:21
jekillen
External User
 
Posts: 1
Default variable substitution


This looks good
Thanks for the suggestion; Jeff K
  Reply With Quote
Reply


Thread Tools
Display Modes




666