PDA

View Full Version : How do I create....


Temper
11-10-2002, 08:08 PM
An else statment inside an else statment. I'm trying to write a code and this is what I have:

<?
switch($solve)
{
case "y" :
echo "<table><tr><td> $X1 x + $Y1 y = $B1 <br>$X2 x + $Y2 y = $B2 </td><td>&nbsp;&nbsp;=</td><td>";
if( $X1 == $X2)
{
$X3 = $X1;
$Y3 = $Y1;
$B3 = $B1;
$X4 = $X2;
$Y4 = $Y2;
$B4 = $B2;
echo "&nbsp;&nbsp; $X3 x + $Y3 y = $B3<br>";
echo "&nbsp;&nbsp; $X4 x + $Y4 y = $B4";
echo "<td>&nbsp;&nbsp;=</td><td>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; $X3 x + $Y3 y = $B3<br>";
echo "&nbsp;&nbsp; - &nbsp;&nbsp; $X4 x + $Y4 y = $B4</td>";
$X5 = $X3 - $X4;
$Y5 = $Y3 - $Y4;
$B5 = $B3 - $B4;
echo "<td>&nbsp;&nbsp;=</td><td>&nbsp;&nbsp; $X5 + $Y5 y = $B5";
echo "<td>&nbsp;&nbsp;=</td><td>&nbsp;&nbsp;";


if( $Y5 == 1 )
}
echo "Y = $B5";
{
else
{
if( $Y5 > 1 )
$B6 = $B5 / $Y5;
echo "Y = $B6";
if( $Y5 < 1 )
$B67 = $B5 / $Y5;
echo "Y = $B6";
}

}
else

{
if( $X1 != $X2)
$X3 = $X2 * $X1;
$Y3 = $X2 * $Y1;
$B3 = $X2 * $B1;
$X4 = $X1 * $X2;
$Y4 = $X1 * $Y2;
$B4 = $X1 * $B2;
echo "&nbsp;&nbsp; $X3 x + $Y3 y = $B3<br>";
echo "&nbsp;&nbsp; $X4 x + $Y4 y = $B4";
echo "</td></tr></table>";
}

break;
case "x" : echo "Y"; break;
}
?>

The red is the problem. How do I change it so that this will work? I always get a parse error, and that's the problem.
Please help me out.
~Mike

mordred
11-10-2002, 08:47 PM
Oh...


if( $Y5 == 1 )
}
echo "Y = $B5";
{


As you said, the above is the source of your error. If you need a code block after an "if" control structure, you have to open it with { and close it with }. Like this


if( $Y5 == 1 ) {
echo "Y = $B5";
}


The rest of your code was not readable for me. Try to make use of indentation, comment and the [php] formatting tags of this forum, and also see
http://www.php.net/manual/en/control-structures.php#control-structures.if

Temper
11-11-2002, 01:51 AM
That helped me quite a bit. Thanks.
Unfortunalty for me, I've run into a new problem:


if( $Y5==1 )
{
echo "Y = $B5";
}

if( $Y5>1 )
{
$B6 = $B5 / $Y5;
echo "Y = $B6";
}
if( $Y5<0 )
{
$B6 = $B5 / $Y5;
echo "Y = $B6";
}


My problem here is with the:
if( $Y5<0 )
{
$B6 = $B5 / $Y5;
echo "Y = $B6";
}

I'm making a type of calculator, and every time that $Y5 is a negative number, it won't calculate. For example, if I have
-3 x + 0 y = 3, ( x+y=b) for an equation, the next step is to devide the b value by the x ( I want it to divide 3 by -3 to give a -1). Unfortunatly for me, when I run my program, it dosen't divide, and just gives me an anwser of +3. I think the problem is that my program isn't recognizing the fact that the x value is negative. Is there some way other then " if( $Y5<0 ) " to make my program recognize negative numbers?
Thank you
~Mike

mordred
11-11-2002, 08:02 AM
That works as expected for me, and it's a correct way to detect negative numbers.


$Y5 = -3;
$B5 = 3;

if( $Y5 < 0 ) {
$B6 = $B5 / $Y5;
echo "Y = $B6";
}


gives me "Y = -1"... perhaps your variables are not set somewhere in your script? Use error_reporting(E_ALL) at the top of your script to find out if you're working on an undefined variable.