Jump to content

How to check for a variable with a certain value?


ageattack

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. 

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) {
$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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.