RE: Variables and Strings

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> 
> Thanks for the responses guys, but what i'm saying is i would 
> like to return
> all the variable names i have in a string,
> $String="Blah Blah Blah $VarName Blah Blah Blah";
> $Vars=myspecialfunction($Varname);
> echo ($Vars);
> 
> that code would produce $Varname, if there were more 
> variables it would also
> return the Variable Names in an array

I believe what Jochem is trying to tell you is that when you assign the
string variable, the variable names are expanded to their values, so the
names are no longer available.

Consider this:

$partname='widget';
$amount=100;

$string="This $partname costs $amount dollars";

/*
$string now equals "This widget costs 100 dollars", so when you pass it to
your function, it has no way to know for sure what the variables were, or
that there even were ANY variables involved in the generation of the string.

The variable NAMES are not passed because PHP expands them to their values
and sets $string accordingly.
*/

$string='This $partname costs $amount dollars';

$string now equals "This $partname costs $amount dollars".  In this case, I
used single quotes.  The values are not expanded and the variable NAMES are
passed.  The VALUE of the variables are not.  So you could then have a
function look for any word that starts with a $ and return an array of
names.

That all being said, what in the world are you trying to do with this?  I
bet you $1.78 (the change in my desk drawer) there is a much better way to
solve whatever problem it is than the way you are trying here.

JM

Because it ruins the flow of the conversation.
>Why is top posting a bad thing?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux