Forum Moderators: coopster

Message Too Old, No Replies

how to have a default value for a variable if conditions are not met

         

mack

7:29 am on Oct 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I have a script with about 500 rows that work out a value for a posted value.

$t1 = $value-167;
$t2 = $value-166;
$t3 = $value-165;
$t4 = $value-164;
$t5 = $value-163;
$t6 = $value-162;
$t7 = $value-161;
$t8 = $value-160;
$t9 = $value-159;
$t10 = $value-158;
$t11 = $value-157;

In the above code $value is preset within the script, the $tx part derives it's value from the $value string. This works well, but depending on the value of $value the value of the $tx can go out with the ranges I need.

What I want to do is set limits. The base limit will be 0 and the upper limit will be 16000. I would like the value to be set to 16000 if the value $tx goes over 16000 or below 0. I know this could be done on a per line basis by adding an if statementm but I have literaly got hundreds of lines of code I would need to work though.

Thank you very much in advance.

Mack.

acemaster

9:20 am on Oct 25, 2008 (gmt 0)

10+ Year Member



Does it need to have separate variables or can you just use an array? Something like:


$t = array();
for ($i=0; $i<$rows; $i++){
$t[$i] = $value - $i;
if ($t[$i] > 16000 ¦¦ $t[$i] < 0) $t[$i] = 16000;
}

mack

11:30 am on Oct 25, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



acemaster, thank for the reply...

Each $tx value will later be called indevidualy. I am guessing the code would need to e somethign like...

if ($t%% != >1) $$ ($t%% != <1600)
{
$t&& = "1600";
}

The code above is just an exaple of how I imagine it should work, and is not valid php :)

Thanks again.

Mack.

cameraman

3:47 pm on Oct 25, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it absolutely has to be separate variables, you can check them with PHP's variable variable feature:
$xmax = 11; // upper value of x
for($i = 1; $i <= $xmax; $i++) {
$vbl = "t$i";
if($$vbl < 0) $$vbl = 0;
if($$vbl > 16000) $$vbl = 16000;
}