Check if the string is a variable or just a string

I have a variable called $Title

It is possible that the variable contains a string,

Example A: 'Foo'

But a variable may also contain a reference to another variable,

Example B: '$ Foo'

When I use print $Title , php returns "Foo" (EX A) or "$ Foo" (EX B) as a 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 do the following: When $Title contains only a string, print this line When $Title contains a link to a variable, find this variable and show its contents

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

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

In this case, $Foo and $Bar are variables and should act as such, "Bar" and "Foo" are strings and should act as such.

How can I make this work?

+4
source share
6 answers

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

Case 1, a simple line:

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

Case 2, "variable in line":

 $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 line with a dollar in it:

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

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

You cannot have "variables in rows". Writing "$foo" immediately interpolates the value of $foo and returns a new line.


Just maybe you want this:

 $foo = 'bar'; // the string "bar" $baz = '$foo'; // the string "$foo" // MAGIC echo $baz; // echoes "bar" 

Ie, if your string contains a dollar followed by a variable name, you want to replace that value. First, I would say that this is a bad idea. Then I would say that you need to extract all these "dollar strings" from your string, check if the variable exists, and then replace the value in the string using the usual string manipulation. Yes, you could do it using eval , but no, this is not a good idea. For the above code, something like this will do:

 if ($baz[0] == '$') { $varName = substr($baz, 1); if (isset($$varName)) { $baz = $$varName; } } 
+6
source

is_string The PHP function is used to check if a value is a string. This can be used in the if () statement to process strings in one way and without strings in another. It will return true or false.

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

The above code should output "No" because 23 is not a string. Try again:

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

Since "Hello World" is a string, it will be an echo of "Yes."

+3
source

Use the 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
source

Code like this:

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

cannot be evaluated when trying to print it, it is evaluated at the time of appointment. The names of the cause variables are not replaced by their values; in your case, 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 

Please note that in the second scenario it only processes variable names, it does not run the code ("+" is a string, not an operation).

If you want to save '$a + $b' as a string in $title and evaluate it as PHP code at the moment when you print it, you need to use eval . However, I highly recommend trying to avoid using this feature as much as possible.

0
source

As I understand it, a string can be just a string or some kind of "reference" variable. Will this work for you or will there always be $ if the variable is a reference?

 $var1='test'; $ref1='var1'; if(isset($$ref1)) { // variable exists } else { // no such variable } 
0
source

You can use dangerous eval php contruct. But be careful if any line comes from 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]

0
source

All Articles