1

I'm trying to make a general if for some variables that i have in my database. Let's say that we have the next variables and values:

$var1 = "abc";
$var2 = "n/a";
$var3 = "das";
$var4 = "n/a";

My if statement must include all these variables, check which variables contain "n/a" and for every variable that contain "n/a" the script must make another variable $var21 = "Not Available". Is that possible?

2
  • you can set up a default value inside your database. Maybe it will do the trick Commented Apr 6, 2012 at 13:51
  • Sure it's possible - why do you need to do this? Commented Apr 6, 2012 at 13:54

2 Answers 2

1

I'd not use variable variables, just due to personal preference. If you're masochistic, you can certainly try.

I'd create an extra hash instead. This would remove the need for an if-statement as well. :)

$descriptions = array(
    "n/a" => "Not Available",
    "brb" => "Be Right Back",
    "iee" => "I'm easily extendable"
);

Then when you need the text for a particular key you can just type

$descriptive_text = $descriptions[$var1];

Updated:

To set it to the original if there is no value in $descriptions, do the following:

$descriptive_text = (is_null($descriptions[$var1])) ? $var1 : $descriptions[$var1];
Sign up to request clarification or add additional context in comments.

1 Comment

and if the variable not contains "n/a" how can i go with the actual value?
0

You should use arrays for this.

$array = array("abc", "n/a", "das", "n/a");
foreach($array as $key => $value)
{
 if($value == "n/a")
 {
  echo "Do Something!";
 }
}

See also: Arrays.

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.