[英]Got fatal error in PHP array
$all = array($stu_quiz_1, $stu_quiz_2, $stu_quiz_3);
$length = count($all);
$low = 10;
$lowest = 0;
for($i = 0; $i<$length; $i++){
if($all($i)<= $low){ // line 34
$lowest = all($i);
}
else{
continue;
}
return $lowest;
}
I am new at php so please help me to find it. 我是php的新手,请帮助我找到它。 I just want to get lowest value from this code.
我只想从此代码中获取最低价值。 I have three values like
$stu_quiz_1 = 20
, and so on ...it shows: 我有三个值,例如
$stu_quiz_1 = 20
,依此类推...它显示:
Fatal error: Function name must be a string in C:\\xampp\\install\\htdocs\\just\\quiz_handle.php on line 34
致命错误:函数名称必须是第34行上C:\\ xampp \\ install \\ htdocs \\ just \\ quiz_handle.php中的字符串
if($all($i)<= $low){ // line 34
$all
is not a function so you can't use parentheses. $all
不是函数,因此不能使用括号。 You'll have to use square brackets []
to access the array value. 您必须使用方括号
[]
来访问数组值。
伙计...不是$all($i)
,而是$all[$i]
。
Assuming your function is called all
then your if
clause should be called like below 假设您的函数
all
被调用,那么您的if
子句应如下调用
if(all($i)<= $low){ // line 34
Note the missing $
from the beginning, thus the name is a string
and not a variable. 请注意,开头缺少
$
,因此名称是一个string
而不是一个变量。
This line : 这行:
$lowest = all($i);
Means that you're calling the function all() with $i as a parameter. 意味着您正在使用$ i作为参数调用函数all()。
But you $all is actually an array not a function so to access an element of an array you use []
. 但是$ all实际上是一个数组,而不是一个函数,因此您可以使用
[]
访问数组的元素。
So you have to change it to : 因此,您必须将其更改为:
$lowest = $all[$i];
First: Change $all($i)
to $all[$i]
(on line 34) 首先:将
$all($i)
更改$all($i)
$all[$i]
(第34行)
Second: Change $lowest = all($i);
第二:更改
$lowest = all($i);
(below the line 34) with $lowest = $all[$i];
(在第34行以下),其中
$lowest = $all[$i];
. 。 In this you were missing a
$
sign in front of all
and $i
was to be kept inside []
because $all
is a variable (containing an array). 在这种情况下,您在
all
前面都缺少$
符号,并且$i
被保留在[]
内部,因为$all
是变量(包含数组)。
To simply get highest or lowest values from an array there are some perfectly suited functions built into the core of PHP - namely max
and min
. 为了简单地从数组中获取最高或最低值,PHP的核心内置了一些非常合适的函数,即
max
和min
。
$all=array( 0,1,23,99,34,838 );
$lowest = min( $all );
$highest= max( $all );
echo $lowest,', ', $highest;
/* output: 0, 838 */
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.