[英]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. 我制作了一个简单的.php文件,该文件应该为我“画一条线”,用户可以使用表格为其指定起点和终点。 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) 我收到错误提示说
syntax error, unexpected '$x0' (T_VARIABLE) in /file.php on line 6
(这是返回行)的syntax error, unexpected '$x0' (T_VARIABLE) in /file.php on line 6
)
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. 编辑我确实看过这篇文章 (“ PHP解析/语法错误;以及如何解决它们?”),但答案对我而言并不存在。 At least I couldn't find it.
至少我找不到。
You're using wrong minus character. 您使用了错误的减号。 You're using
–
(char code 8211) and minus is -
(char code 45). 您正在使用
–
(字符代码8211),而负号是-
(字符代码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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.