html - How do I create a PHP function to output values from different variables when called? -


i have different variables arrays different values. need display values of each array item in html, know how do. problem is, need find percentage of values of each array item in relation themselves. equation like:

blue balls percentage = blue balls / sum of array items * 100

i'm way use function in order echo results of each array item through equation, have no idea how write it.

this arrays:

$balls = array(     'red' => 5,     'blue' => 7,     'green' => 2 ); $socks = array(     'red' => 5,     'blue' => 7,     'green' => 2 ); 

this think html should like:

blue balls make up<?php echo $percent($ball['blue']); ?>% of balls.  there <?php echo $balls['blue']; ?> blue balls. 

i'm trying achieve final result:

blue balls make 50% of balls. there 7 blue balls. 

i know bottom echo valid, have no idea how attempt write function top echo, or if echo possible.

<?php  $balls = array(     'red' => 5,     'blue' => 7,     'green' => 2 ); $socks = array(     'red' => 5,     'blue' => 7,     'green' => 2 );  function percent($array, $element) {     $total = 0;     $elementvalue = $array[$element];     foreach($array $entry)     {         $total += $entry;     }     return ($elementvalue / $total) * 100; } ?> <html> <body>     blue balls make up<?php echo percent($balls, 'blue'); ?>% of balls.      there <?php echo $balls['blue']; ?> blue balls. </body> </html> 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -