Jump to content

Is It Possible To Ignore A Variable?


Shadowing

Recommended Posts

I was wondering if it was possible to have php simply act like a variable doesnt existwhat im trying to do below is. if naq equals 0 then it will ignore naq and only find the minium of the 2 variables. Much appreicated if anyone has any ideas

 <?php  if ($naq == 0) {	  	  $naq = null; 	 	 	 $max = min(array($sym, $res, $naq));	 }?> 

Link to comment
Share on other sites

something like

if ($naq == 0) { $max = min(array($sym, $res));}else{         $max = min(array($sym, $res, $naq));         }

Link to comment
Share on other sites

i dont want it to be used when it equals 0 other wise i want it to be used
Your if statement would do exactly what yo want it to do.
<?phpif($var != 0){//Type the code you want to run here when the variable DOESN'T equal 0.}else{//The the code you want to run when the variable DOES equal 0.}?>

If the variable doesn't 0 it will run the first code and ignore what is inside the else.If the variable is equal to 0 it will ignore the first bit of code and only run what is inside the else statement. ~Krewe

Link to comment
Share on other sites

if 0 could be the value of it what is the problem to include it in array? if 0 is the lowest value it will return it. if there is any other background of your problem you can elaborate it to us.

Link to comment
Share on other sites

Thanks for all the responces guys,sorry i replied before reading birbal's reply. the problem is any one of those values in the array could be 0 also. so it would be alot of if statements and i have 7 of these to do.
So? It isn't that much more work.
<?php if ($naq == 0) {$max = min(array($sym, $res));}elseif($sym = 0){$max = min(array($res, $naq));}elseif($res = 0){$max = min(array($sym, $naq));}else{$max = min(array($sym, $res, $naq));} ?>

He might be trying to exclude any nulls or empties. Like if nothing happened, or if the value of the question/statement happened to be 0, he might not want it in his data. ~Krewe

Link to comment
Share on other sites

i cant have the number be the minium number. There are 3 differant types of resources to build a unit.Im figuring out the max amount of that unit a person can purchase by finding the max of each resource first then grabing the minium one. If i ever wanted to change a val to 0 "which means that unit doesnt need any of that resource" the 0 would become the lowest value and the max units you could build would be 0 I rewrote a easy related example

 <?php	    $gold = 100;$iron = 80;$wood = 200; $gold_val = 10;$iron_val = 5;$wood_val = 0;  	   $g = @floor($gold / $gold_val); 	   $i = @floor($iron / $iron_val);	   $w = @floor($wood / $iron_val);	 	 $max = min(array($g, $i, $w)); echo $max;?> 

Link to comment
Share on other sites

i cant have the number be the minium number. There are 3 differant types of resources to build a unit.Im figuring out the max amount of that unit a person can purchase by finding the max of each resource first then grabing the minium one. If i ever wanted to change a val to 0 "which means that unit doesnt need any of that resource" the 0 would become the lowest value and the max units you could build would be 0 I rewrote a easy related example
 <?php	   $gold = 100;$iron = 80;$wood = 200; $gold_val = 10;$iron_val = 5;$wood_val = 0;  	   $g = @floor($gold / $gold_val);	   $i = @floor($iron / $iron_val);	   $w = @floor($wood / $iron_val);		 $max = min(array($g, $i, $w)); echo $max;?> 

Ah, well...First you can not divide by 0. So if you have a value that is equal to zero you will want to find that before you divide.
<?php$gold = 100;$iron = 80;$wood = 200; $gold_val = 10;$iron_val = 5;$wood_val = 0; if($gold_val =  0){  $i = @floor($iron / $iron_val);  $w = @floor($wood / $wood_val);  $max = min(array($i, $w));}elseif($iron_val = 0){  $g = @floor($gold / $gold_val);  $w = @floor($wood / $wood_val);  $max = min(array($g, $w));}elseif($wood_val = 0){  $g = @floor($gold / $gold_val);  $i = @floor($iron / $iron_val);  $max = min(array($g, $i));}else{  $g = @floor($gold / $gold_val);  $i = @floor($iron / $iron_val);  $w = @floor($wood / $iron_val);  $max = min(array($g, $i, $w));} echo $max;?>

Now since this is obviously a game as I can tell, you will need to check if $max is >= 1, if not you want to make it so they can not build since they can't even build a full item. ~Krewe

Link to comment
Share on other sites

you can is use exeception to catch infinite math exception or you can check the value of $wood_val and others in if-else if its zero assighn $w=0 else $w = @floor($wood / $iron_val);

Link to comment
Share on other sites

Ah, well...First you can not divide by 0. So if you have a value that is equal to zero you will want to find that before you divide.
$gold = 100;$iron = 80;$wood = 200; $gold_val = 10;$iron_val = 5;$wood_val = 0; if($gold_val =  0){  $i = @floor($iron / $iron_val);  $w = @floor($wood / $wood_val);  $max = min(array($i, $w));}elseif($iron_val = 0){  $g = @floor($gold / $gold_val);  $w = @floor($wood / $wood_val);  $max = min(array($g, $w));}elseif($wood_val = 0){  $g = @floor($gold / $gold_val);  $i = @floor($iron / $iron_val);  $max = min(array($g, $i));}else{  $g = @floor($gold / $gold_val);  $i = @floor($iron / $iron_val);  $w = @floor($wood / $iron_val);  $max = min(array($g, $i, $w));} echo $max;?>

Now since this is obviously a game as I can tell, you will need to check if $max is >= 1, if not you want to make it so they can not build since they can't even build a full item. ~Krewe

When I devide by 0 i get a return of 0 which is what I want. When $wood_val changes to 0 im saying that a unit doesnt need to even use wood to build that unit. so it shouldnt be included in the min (array) what im doing is I made a global setting page which allows me to make game changes from a webpage with out actually editing the script. $wood_val is the cost of wood for that unit. So if the cost of wood for that unit is 0 and the player has enough of the other two resources "which is $g and $i it shows the max he can build. just in case in the future during testing i decide a unit no longer needs to use that resource
Link to comment
Share on other sites

When I devide by 0 i get a return of 0 which is what I want. When $wood_val changes to 0 im saying that a unit doesnt need to even use wood to build that unit. so it shouldnt be included in the min (array) what im doing is I made a global setting page which allows me to make game changes from a webpage with out actually editing the script. $wood_val is the cost of wood for that unit. So if the cost of wood for that unit is 0 and the player has enough of the other two resources "which is $g and $i it shows the max he can build. just in case in the future during testing i decide a unit no longer needs to use that resource
You might be getting 0 now, but it might cause trouble later.You can not divide by 0, simple math... It isn't possible. Also... provide feedback on the help you are getting. You are not letting us know if we are helping you. Or if we gave you your answer.
Link to comment
Share on other sites

yah thanks alot for the help I'll either use if else statements or a huge number to make it false. I guess the long number idea is just as long as the if else statements almost. Either way if your saying i'll have issues in the future cause im deviding by 0 it really makes me need to think of another way of doing this. But I dont think it exists. so you are saying that one day php will simply not say its 0? Guess i dont understand. This is a very rare situation honestly. I could just do this for game testing only and edit it out later on.

 <?php $sym = floor($symbiotes / $sym_val);			  if ($sym == 0) { $sym = 100000000000;}	  $res= floor($resources / $res_val);			 if ($res == 0) { $res = 100000000000;}	  $naq = @floor($naqahdah / $naq_val);			  if ($naq == 0) { $naq = 100000000000;}  	 $max = min(array($sym, $res, $naq));?> 

Link to comment
Share on other sites

yah thanks alot for the help I'll either use if else statements or a huge number to make it false. I guess the long number idea is just as long as the if else statements almost. Either way if your saying i'll have issues in the future cause im deviding by 0 it really makes me need to think of another way of doing this. But I dont think it exists. so you are saying that one day php will simply not say its 0? Guess i dont understand. This is a very rare situation honestly. I could just do this for game testing only and edit it out later on.
 <?php $sym = floor($symbiotes / $sym_val);			  if ($sym == 0) { $sym = 100000000000;}	  $res= floor($resources / $res_val);			 if ($res == 0) { $res = 100000000000;}	  $naq = @floor($naqahdah / $naq_val);			  if ($naq == 0) { $naq = 100000000000;}  	 $max = min(array($sym, $res, $naq));?> 

No... there is no need to change the way. What is wrong with my example that makes you think it won't work?It checks to see if any of the value for the item are 0. If they are they ignore it, since it doesn't matter. It is just a safe way to make sure you don't divide by zero.If no values are 0 then it divides all of them. My example should work for you, so what is wrong?
Link to comment
Share on other sites

... I am confused, is my example not working for you? It is wrong?It should be working just fine. All it is is a simple if else/if statement. All it is doing is checking if the value is 0.If the value is zero there is no reason the code needs to worry about it, so it ignores it.If the value of all the fields are not equal to zero it will uses all the fields and find the min out of them all. Please explain why you are so against using the if else statements... ~Krewe

Link to comment
Share on other sites

I was looking at the manual and I was thinking the script would have to know you are deviding by 0 so it would return 0 anyways. Cause it would have to know its 0 in order to flip the errorthis was from the manual <?phpif ($divisor == 0) { trigger_error("Cannot divide by zero", E_USER_ERROR);}?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...