Kasper Kasper - 7 months ago 10
PHP Question

What's wrong with my simple line 'drawing' code?



I made a simple .php file that should 'draw' a line for me, for which the user gives the start and end points using a form. And by 'draw', I mean tell you the pixels it's coloring. And yes, I know this only works with very specific lines.

This is my entire file:

<html>
<head>
<title>Thing</title>
<?php
function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
return (($x1–$x0)*$y – ($y1–$y0)*$x – $x1*$y0 + $x0*$y1);
}
?>
</head>
<body>
<form name="Q1" method="post" action="<?php $_SERVER["PHP_SELF"] ?>">
<table>
<tr>
<td align="right">x0</td>
<td><input type="number" name="x0" value="<?php echo $_POST["x0"]; ?>"></td>
</tr>
<tr>
<td align="right">x1</td>
<td><input type="number" name="x1" value="<?php echo $_POST["x1"]; ?>"></td>
</tr>
<tr>
<td align="right">y0</td>
<td><input type="number" name="y0" value="<?php echo $_POST["y0"]; ?>"></td>
</tr>
<tr>
<td align="right">y1</td>
<td><input type="number" name="y1" value="<?php echo $_POST["y1"]; ?>"></td>
</tr>
</table>
</form>
<?php
if (!empty($_POST)){
$x0 = $_POST["x0"];
$x1 = $_POST["x1"];
$y0 = $_POST["y0"];
$y1 = $_POST["y1"];

$y = $y0;
for($x = $x0;$x <= $x1; $x++){
echo "Step $x | x=$x | y=$y"
if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
$y++;
}
}
}
?>
</body>




I'm getting and error that says
syntax error, unexpected '$x0' (T_VARIABLE) in /file.php on line 6
(which is the return line)

But I don't understand what's the problem.


EDIT I have indeed taken a look at this post ("PHP Parse/Syntax Errors; and How to solve them?") but the answer wasn't there for me. At least I couldn't find it.

Answer

You're using wrong minus character. You're using (char code 8211) and minus is - (char code 45). I don't know how you had this different character, but changing it will work.

<?php
        function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
            return (($x1 - $x0) * $y - ($y1 - $y0) * $x - $x1 * $y0 + $x0 * $y1);
        }
?>

But then it pointed an error right here:

for($x = $x0;$x <= $x1; $x++){
    echo "Step $x | x=$x | y=$y"
    if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
        $y++;
    }

You're missing a semi-colon ; after the echo, above the if.

Full working code:

<html>
<head>
    <title>Thing</title>
    <?php
        function positiveLowLine($x,$y,$x0,$x1,$y0,$y1){
            return (($x1 - $x0) * $y - ($y1 - $y0) * $x - $x1 * $y0 + $x0 * $y1);
        }
    ?>
</head>
<body>
    <form name="Q1" method="post" action="<?php $_SERVER["PHP_SELF"] ?>">
        <table>
            <tr>
                <td align="right">x0</td>
                <td><input type="number" name="x0" value="<?php echo $_POST["x0"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">x1</td>
                <td><input type="number" name="x1" value="<?php echo $_POST["x1"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">y0</td>
                <td><input type="number" name="y0" value="<?php echo $_POST["y0"]; ?>"></td>
            </tr>
            <tr>
                <td align="right">y1</td>
                <td><input type="number" name="y1" value="<?php echo $_POST["y1"]; ?>"></td>
            </tr>
        </table>
    </form>
    <?php
        if (!empty($_POST)){
            $x0 = $_POST["x0"];
            $x1 = $_POST["x1"];
            $y0 = $_POST["y0"];
            $y1 = $_POST["y1"];

            $y = $y0;
            for($x = $x0;$x <= $x1; $x++){
                echo "Step $x | x=$x | y=$y";
                if (positiveLowLine($x,$y,$x0,$x1,$y0,$y1) < 0){
                    $y++;
                }
            }
        }
    ?>
</body>
Comments