Finding Errors in PHP Array Code
In the following tasks, a programmer wrote some code and possibly made mistakes in it. You must check if the code does what is described. If the code works incorrectly, you must fix the errors.
Задание 1
The code should output the length of the array:
php<?php
$arr = [1, 2, 3, 4, 5];
echo strlen($arr);
?>
Задание 2
The code should output the last element of the array:
php<?php
$arr = [1, 2, 3, 4, 5];
echo $arr[count($arr)];
?>
Задание 3
The code should find the sum of the array elements:
php<?php
$arr = [1, 2, 3, 4, 5];
echo $arr[1] + $arr[2] + $arr[3] + $arr[4] + $arr[5];
?>
Задание 4
The code should output the length of the array:
php<?php
$arr = [1, 2, 3, 4, 5];
echo count([$arr]);
?>
Задание 5
The code should find the sum of the array elements:
php<?php
$arr = [
'a' => 1,
'b' => 2,
'c' => 3,
];
echo $arr[a] + $arr[b] + $arr[c];
?>
Задание 6
The code should output an array element by the key specified in the variable:
php<?php
$arr = [
'a' => 1,
'b' => 2,
'c' => 3,
];
$k = 'a';
echo $arr['$k'];
?>
Задание 7
The code should output the length of the array:
php<?php
$arr = [1, 2, 3, 4, 5];
echo count($arr);
?>
Задание 8
The code should output an array element by the key specified in the variable:
php<?php
$arr = [
'a' => 1,
'b' => 2,
'c' => 3,
];
$a = 1;
echo $arr[$a];
?>