# How to check for a variable with a certain value?

Go to solution Solved by Ch0cu3r,

## Recommended Posts

I want an if statement that not only checks for the existence of a variable, but which also checks all the variables already created for one with a specific value. For example, I have a loop which creates variables, but I don't want it to make two variables with the same value. The problem is, using

```if(isset(\${'h'.\$x}==false) && \${'h'.\$x} != 3{

}
```

won't work since the variable equal to 3 could be named h4, whereas this one will be named h5. I hoped I explained this efficiently enough, sorry about any confusion.

##### Share on other sites

Can you show us this code?  Not catching your statement "have a loop that creates variables".

##### Share on other sites

You're pretty close actually.

But don't use variable variables. If you want arbitrary variables then you should be using an array with arbitrary keys instead. Then

`if(isset(\$array['h'.\$x])==false && \$array['h'.\$x] != 3) {`
##### Share on other sites

```\$count = count(\$bytes);
for(\$x=0; \$x<\$count; \$x++){
\${'h' . \$x} = substr(\$bytes[\$x],8,-;
\$array1[\${'h' . \$x}] = array();
\$array2[\${'h' . \$x}] = array();
\$array3[\${'h' . \$x}] = array();
\$array4[\${'h' . \$x}] = array();
\$array5[\${'h' . \$x}] = array();
\$offset[\${'h' . \$x}] = array();
\$avg[\${'h' . \$x}] = array();
\$avg2[\${'h' . \$x}] = array();
\$percentage[\${'h' . \$x}] = array();
\$values[\${'h' . \$x}] = null;

}

```

This is my code, where \$bytes is an array containing the preg_match_all. The thing is, sometimes there is the same matchable string twice, meaning that it will create the same array key 2 times. Also, there will be two different variables, such as h2 and h13, which will have the same value. That doesn't screw the program up at all, but it makes a hell of a lot of error messages pop up. I just want to make sure that there are no \$hx variables with the same value.

P.S. I apologize if my code looks like crap. I'm still learning.

Edited by ageattack
##### Share on other sites

• Solution

What is it you are trying to do? Variable variables inst efficient way of grouping data. That is what arrays are for.

If you are only wanting to get unique values then store your data in an array and use array_unique to get only the unique values.

##### Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.