Forum Moderators: coopster
Ime trying to get a new variable from a old variable, the new var has to be equal, -1 or +1, but not smaller than 1, or bigger than 3.
somehow this script only results in a 2 or 3
--------
if ( $data[4] = 1 ) { $pol_max=2 ; $pol_min=1 ; }
if ( $data[4] = 2 ) { $pol_max=3 ; $pol_min=1 ; }
if ( $data[4] = 3 ) { $pol_max=3 ; $pol_min=2 ; }
$newvar=rand($pol_min,$pol_max)
---------
while this script only produces 1 or 2
if ( $data[4] = 1 ) { $pol_max=2 ; $pol_min=1 ; }
elseif ( $data[4] = 2 ) { $pol_max=3 ; $pol_min=1 ; }
elseif ( $data[4] = 3 ) { $pol_max=3 ; $pol_min=2 ; }
also tried the line below as replacement for the lastline above;
else { $pol_max=3 ; $pol_min=2 ; }
$newvar=rand($pol_min,$pol_max)
---------
What ame i doing wrong?
-----
if ( $data[3] == 1 ) { $pol_max=2 ; $pol_min=1 ; }
elseif ( $data[3] == 2 ) { $pol_max=3 ; $pol_min=1 ; }
else {if ( $data[3] == 3 ) { $pol_max=3 ; $pol_min=2 ; } }
------
greetZ
Although it ultimately achieves the same thing you should look at using a switch here. It's a much simpler/cleaner way of organising this kind of code.
switch ($value){
case 1:
$pol_max=2;
$pol_min=1;
break;
case 2:
$pol_max=3;
$pol_min=1;
break;
case 3:
$pol_max=3;
$pol_min=2;
break;
default:
echo "none of the cases were matched";
}
Cheers
sure i did this before, must have missed a "{}"
Unlikely (that would almost certainly give you a parse error). I think you know this now, but just to make sure.... Mixing up the assignment and the comparison operator is one of the first things to look for when a conditional (whether it be in an if, while, or whatever) does not evaluate as expected.
if ( $data[4] = 1 ) { $pol_max=2 ; $pol_min=1 ; }
if ( $data[4] = 2 ) { $pol_max=3 ; $pol_min=1 ; }
if ( $data[4] = 3 ) { $pol_max=3 ; $pol_min=2 ; }
This will ALWAYS result in
$pol_max=3 ;
$pol_min=2 ;
because you are using an assignment operator, not a comparison operator. The third condition is always evaluated and is always true because $data[4] is not equal to zero (and 0 is the same as FALSE and any nonzero value is the same as true). That is
$data[4] = 0;
if ($data[4] = 0 ) { do something }
else {do nothing}
always results in doing nothing, because the condition always evaluates to false.
In your second example
if ( $data[4] = 1 ) { $pol_max=2 ; $pol_min=1 ; }
elseif ( $data[4] = 2 ) { $pol_max=3 ; $pol_min=1 ; }
elseif ( $data[4] = 3 ) { $pol_max=3 ; $pol_min=2 ; }
The result will always be
$pol_max=2 ;
$pol_min=1 ;
because your first conditional ($data[4] = 1) is always true in all cases, so you never get to the else clauses.
Tom
ex.
if () {}
elseif () {}
else { if () {}
elseif () {}
else { if () {}
.....no troublez..except for keeping the count on the closing "}"'s , i have it working with 4 "else {" statements now , is there a limit?
Since you seem to know your php stuff, i have another question;
At this point ime using a total of 4 different data.txt files, i'me thinking of using a access db, (and php cant use it (isp)), ive done some testing, but i need to be sure, can i use $vars posted from a php page, with .asp and store them?
GreetingZ,
Jordan
could case less where it came from
And doesn't even know. I'm not sure why, but this seems to be a hard concept for people to understand about PHP (or Perl or whatever). Once the page is parsed and turned into HTML, it IS just HTML and the browser has no idea that the page ever was in PHP, so when you fill in a form and POST it, it's exactly as though the form had been written in HTML.