Forum Moderators: coopster

Message Too Old, No Replies

if / ifelse / and statement , +$var's

if else and, keeps last var?

         

NL_Cartman

10:52 pm on Sep 26, 2003 (gmt 0)

10+ Year Member



Hi!,

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?

NL_Cartman

9:10 am on Sep 27, 2003 (gmt 0)

10+ Year Member



after playing to long with this easy script, i have it working now; (sure i did this before, must have missed a "{}"?) ;

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

mogwai

10:55 am on Sep 27, 2003 (gmt 0)

10+ Year Member



Hi,

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

NL_Cartman

11:56 pm on Sep 27, 2003 (gmt 0)

10+ Year Member



thnx

willybfriendly

1:37 am on Sep 28, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Perhaps another approach to the same problem?

if($data[4]<4 && $data[4] >0)
{
$pol_min = $data[4] - 1;
if($pol_min < 1)
$pol_min = 1;
$pol_max = $data[4] + 1;
if($pol_max > 3)
$pol_max = 3;
}

WBF

NL_Cartman

11:56 am on Oct 7, 2003 (gmt 0)

10+ Year Member



thnx willy,

this would seem to be the cleanest code indeed! :)

greetingZ

ergophobe

4:07 pm on Oct 7, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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

NL_Cartman

9:00 pm on Oct 8, 2003 (gmt 0)

10+ Year Member



thnx for explaining me/us to the "Why" my if's didn't work, would you know if there's a limit to the else {}

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?

ergophobe

10:45 pm on Oct 8, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No practical limit, but a debugging nightmare. You might want to get an editor with brace-matching such as
HAPEdit (free)
Kommodo (cheap to expensive depending on licence)
probably EditPad does it (people swear by it, but haven't used it).

Tom

NL_Cartman

9:47 am on Oct 9, 2003 (gmt 0)

10+ Year Member



thanx again Tom!

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,

MonkeeSage

1:12 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



AFAIK, if you have a PHP page with a HTML form on it, and make the action of the form be the path to the asp script (e.g., action="test.asp"), it will work fine, because the POST or GET data is sent via the HTTP protocol and could case less where it came from or where it is going (in terms of scripts and such), it's the exact same data being sent or received in any case.

Jordan

ergophobe

2:52 pm on Oct 9, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




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.

NL_Cartman

8:43 am on Oct 10, 2003 (gmt 0)

10+ Year Member



So Simple, So WonderFull! :)

ThanXxxx!