[英]what's wrong with my php codes?
I wanted to convert my Javascript code for creating a triangle to PHP codes, the Javascript codes works but the PHP code doesn't. 我想将用于创建三角形的Javascript代码转换为PHP代码,该Javascript代码有效,但PHP代码无效。 This is what I have in my PHP codes, I tried to run it but ended up with a fatal error and undefined variable. 这就是我的PHP代码中的内容,我尝试运行它,但最终出现致命错误和未定义的变量。 I understand javascript but not php... 我了解javascript但不了解php ...
<?php
{
$size = $_POST['size'];
$firstChoice = $_POST['firstChoice'];
$secondChoice = $_POST['secondChoice'];
echo "<textarea>";
$allLines = '';
for ( $i = 1; $i <= $size; $i++ )
{
$oneLine = createLine ( $i, $i % 2 ? $FirstChoice : $secondChoice );
$allLines += $oneLine + "\n";
}
echo "$allLines";
function createLine ($size, $symbol) {
$aLine = '';
for ( $j = 1; $j <= $size; $j++ )
{
echo $aLine += $symbol;
}
echo "$aLine";
echo "</textarea>";
}
?>
It should look like this if size = 5
, firstChoice = #
and secondChoice = &
如果size = 5
, firstChoice = #
和secondChoice = &
,则应该看起来像这样
#
&&
###
&&&&
#####
What is $createLine
? 什么是$createLine
? Looks as if you're trying to use it as a function, but it is not defined anywhere. 看起来好像您正在尝试将其用作函数,但未在任何地方定义它。
Edit : 编辑 :
You need to declare the function in php 您需要在php中声明函数
function createLine($size, $symbol) {
// code
}
And when you call it, just call it by the name, don't add a $
. 当您调用它时,只需按名称进行调用,不要添加$
。
$line = createLine($a, $b);
See documentation on php User-defined functions . 请参阅有关php 用户定义函数的文档。
Working : 工作方式 :
There were a few issues including: string concatenation should be using the .
存在一些问题,包括:字符串连接应使用.
operator not +
, a typo in $FirstChoice
, and the function needs to be defined before you use it. 运算符not +
, $FirstChoice
的错字,并且在使用该函数之前需要先定义该函数。
<?php
$size = $_POST['size'];
$firstChoice = $_POST['firstChoice'];
$secondChoice = $_POST['secondChoice'];
function createLine($size, $symbol) {
$aLine = '';
for ($j = 1; $j <= $size; $j++) {
$aLine .= $symbol;
}
return $aLine;
}
echo "<textarea>";
$allLines = '';
for ($i = 1; $i <= $size; $i++) {
$oneLine = createLine($i, $i % 2 ? $firstChoice : $secondChoice);
$allLines .= $oneLine . "\n";
}
echo "$allLines";
echo "</textarea>";
?>
Use createLine(...)
and not $createLine(...)
I suppose you have javascript function like below 使用createLine(...)
而不是$createLine(...)
我想您具有如下所示的javascript函数
<script>
function createLine (...)
{
...
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.