6

I have got a variable called $Title

It is possible that the variable contains a string,

example A: 'Foo'

But the variable can also contain a reference to an different variable,

example B: '$Foo'

When I use print $Title php returns 'Foo' (EX A) or '$Foo' (EX B) as an string.

When I use print $$Title php tries to return the value of a variable named $Foo (EX A) or $$Foo (EX B)

I want to accomplish the following: When $Title contains just a string, print that string When $Title contains the reference to a variable, look up that variable and show its content

I could just look for the first character in the string. When it is $ use echo $$Title ELSE use echo $Title, but it is possible that $Title contains something like this:

$Title = '$Foo . \'Bar\' . $Bar . \'Foo\'';

In that case $Foo and $Bar are variables and need to act as such, 'Bar' and 'Foo' are strings and need to act as such.

How can I make this able to work??

1
  • 1
    Can you put your value in double quotes, instead of single quotes? Commented Dec 19, 2012 at 10:20

6 Answers 6

7

A string is always just a string. A string is never a variable.

Case 1, a plain string:

$foo = 'bar';
echo $foo;  // bar
echo $$foo; // content of $bar if it exists

Case 2, a "variable in a string":

$foo = 'bar';
$bar = "$foo";  // $bar is now the string 'bar', the variable is interpolated immediately
echo $bar;  // bar
echo $$bar; // bar (content of $bar)

Case 3, a string with a dollar in it:

$foo = '$bar';
echo $foo;  // $bar
echo $$foo; // invalid variable name "$bar"

$$foo resolves to the variable name $$bar, which is an invalid name.

You cannot have "variables in strings". Writing "$foo" immediately interpolates the value of $foo and gives you back a new string.


Just maybe, you want this:

$foo = 'bar';   // the string "bar"
$baz = '$foo';  // the string "$foo"

// MAGIC

echo $baz;  // echoes "bar"

I.e., if your string contains a dollar followed by the name of a variable, you want to substitute that value. First I'd say this is a bad idea. Then I'd say you will have to extract all those "dollar strings" out of your string, check if the variable exists, then replace the value in the string using normal string manipulation. Yes, you could do it using eval, but no, that's not a good idea. For the above code, something like this'll do:

if ($baz[0] == '$') {
    $varName = substr($baz, 1);
    if (isset($$varName)) {
        $baz = $$varName;
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

What about writing '$foo'? Doesn't that break your theory somewhat?
No. '$foo' is a string with a dollar in it. It's not a variable.
True, but $foo may be a variable, and '$foo' won't be interpreted as the value of $foo. Just sayin'.
No. '$foo' is the literal string "$foo". "$foo" is a double quoted string in which the variable $foo will be interpolated if it exists. The string will contain the value of $foo, not "$foo" in any shape or form. There are only the three cases given in my answer. A string is a string, there is no "variable string". If you have a string with a dollar in it and want to substitute the value of a variable with the same name as may be present in the string, that's a totally different topic.
That's pretty much what I said. Glad we agree.
3

The is_string PHP function is used to check if a value is a string. This could be used within an if () statement to treat strings in one way and non-strings in another. It will return true or false.

 <?php 
 if (is_string(23)) 
 {
 echo "Yes";
 } else {
 echo "No";
 }
 ?>

The code above should output "No" because 23 is not a string. Let's try this again:

 <?php 
 if (is_string("Hello World")) 
 {
 echo "Yes";
 } else {
 echo "No";
 }
 ?>

Since "Hello World" is a string, this would echo "Yes".

Comments

1

Use a if statement to check if the $variable is a string..

if(is_string($var)) { 
   echo $var; 
} else { 
  // What do you want  to achieve here?
} 

1 Comment

I was also thinking about that
0

Code like this:

$Title = '$Foo . \'Bar\' . $Bar . \'Foo\'';

can't be evaluated when you try to print it, it's evaluated at the moment of assignment. The reason variable names are not being replaced by their values in your case are single quotes.

$a = 1;
$b = 2;
$var = '$a + $b'; // this is a string
echo $var; // $a + $b
$var = "$a + $b"; // this is also a string, but variables will be processed
echo $var; // 1 + 2

Note, that in second scenario it only processes the variable names, it doesn't run the code ('+' is a string, not an operation).

If you want to keep the '$a + $b' as a string within your $title and evaluate it as a PHP code at the moment, when you print it, you need to use eval function. However, I strongly suggest trying to avoid using this function as much as possible.

Comments

0

As I can understand, string may be just string or some sort of variable 'reference'. Will this work for you or there is always $ if variable reference?

$var1='test';
$ref1='var1';

if(isset($$ref1)) {
    // variable exists
}
else {
    // no such variable
}

Comments

0

You can use the dangerous eval php contruct. But be warned if any of the string is coming from a user input

 
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";

This is just a copy and paste from (PHP eval documentation)[http://php.net/manual/en/function.eval.php]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.