Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if string is variable or just a string

Tags:

variables

php

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

like image 877
AgeDeO Avatar asked Dec 19 '12 10:12

AgeDeO


People also ask

How do you check if a variable is a string in Java?

The Java instanceof keyword is used to check if an object is a certain type. It returns true or false. For example, we can check if a variable is a type of String; we can test classes to see if they are certain types (e.g., is a Birch a Tree or a BoysName?).

How do you check if a variable contains a string in Python?

To check if a variable contains a value that is a string, use the isinstance built-in function. The isinstance function takes two arguments. The first is your variable. The second is the type you want to check for.

How do you check if a variable contains a string in JavaScript?

You can check if a JavaScript string contains a character or phrase using the includes() method, indexOf(), or a regular expression. includes() is the most common method for checking if a string contains a letter or series of letters, and was designed specifically for that purpose.

What is the difference between variables and string?

Variables are symbols that you can use to store data in a program. You can think of them as an empty box that you fill with some data or value. Strings are data, so we can use them to fill up a variable. Declaring strings as variables can make it easier for us to work with strings throughout our Python programs.


2 Answers

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;
    }
}
like image 196
deceze Avatar answered Sep 25 '22 14:09

deceze


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".

like image 43
Ghostman Avatar answered Sep 25 '22 14:09

Ghostman