array_sum function of php:
Array_ sum is used to get the sum of all the values of an array. The array_sum function takes only one parameter that is array. We sum values of that array.
Code:
1. <?php
2. $sum_of_array=array(0=>"10",1=>"20",2=>"30");
3. echo "sum of array values=".array_sum($sum_of_array);
4. echo "<br/>";
5. ?>
Output:
sum of array values=60
Explanation:
On line 2 we have declared and initialize $sum_of_array. On line 3 we have passed that array to array_sum function and print it. As we can see in the output the result is 60 the sum of all values that we initialize in an array.
Code:
1. <?php
2. $sum_of_array=array(0=>10,1=>20,2=>30);
3. echo "sum of array values=".array_sum($sum_of_array);
4. ?>
Output:
sum of array values=60
Explanation:
On line 2 we have declared and initialize $sum_of_array. On line 3 we have passed that array to array_sum function and print it. As we see in the output the result is 60 the sum of all values that we initialize in an array.
Code:
1. <?php
2. $int_string = array(56,"Grades",65);
3. $addition = array_sum( $int_string);
4. print"addition is ". $addition ;
5. echo "<br/>";
6. ?>
Output:
addition is 121
Explanation:
On line 2 we have declared and initialize $int_string. On line 3 we have passed that array to array_sum function and print it. As we can see in the output the result is 121 the sum of all numeric values that we initialized in an array.
Count function of php:
The count () function counts the elements/values indexes of an array. It takes one parameter that is array. We count value of that array using Count($array).
Code:
1. <?php
2. $students = array("Tahreem", "Rizwan", "Khurram", "Jamil");
3. $total_student= count($students);
4. echo "Total number of students are= ".$total_student;
5. echo "<br/>";
6. ?>
Output:
Total number of students are= 4
Explanation:
In the above code on line 2 we have declared an array $students and initialize it. Then we have passed it to count function of php. That count total number of values of array. The result is saved in $total_student. Then we have displayed it.
Code:
1. <?php
2. $students = array(1, 2,3, 4,4,5);
3. $total_student= count($students);
4. echo "Total number of students are= ".$total_student;
5. ?>
Output:
Total number of students are= 6
Explanation:
In the above code on line 2 we have declared an array $students and initialize it. Then we have passed it to count function of php. That count total number of values in an array. The result is saved in $total_student. Then we have displayed it.