Jump to content


Photo

How to check for a variable with a certain value?

php variables isset

Best Answer Ch0cu3r, 05 September 2014 - 10:55 AM

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.

Go to the full post


  • Please log in to reply
4 replies to this topic

#1 ageattack

ageattack
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 September 2014 - 07:37 PM

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. 



#2 ginerjm

ginerjm
  • Members
  • PipPipPip
  • Handball player
  • 4,081 posts
  • LocationVoorheesville NY

Posted 04 September 2014 - 08:08 PM

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


JG
PS - If you're posting here you should be using:
        error_reporting(E_ALL);
        ini_set('display_errors', '1');

at the top of ALL php code while you develop it!

#3 requinix

requinix
  • Administrators
  • Maddening Administrator
  • 9,558 posts
  • LocationWA

Posted 04 September 2014 - 08:30 PM

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

The Reimann Zeta Function Trolley Problem | "Summer is when I, the great ice fairy, can show my true power!"

#4 ageattack

ageattack
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 04 September 2014 - 09:03 PM

$count = count($bytes);
	for($x=0; $x<$count; $x++){
		${'h' . $x} = substr($bytes[$x],8,-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, 04 September 2014 - 09:05 PM.


#5 Ch0cu3r

Ch0cu3r
  • Moderators
  • Advanced Member
  • 3,418 posts

Posted 05 September 2014 - 10:55 AM   Best Answer

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.






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users