I am a starter in web development. My requirement in a bit unique here. I have a set of scores, score_1 to score_n. Each score is associated with a factor with which it affects the grand score. Now I wish to write a configuration.php file as:
<?php
define("NUMBER_SCORES",4,true);
define("FACT_SCORE[1]",0.2,TRUE);
define("FACT_SCORE[2]",0.6,TRUE);
define("FACT_SCORE[3]",0.8,TRUE);
define("FACT_SCORE[4]",0.6,TRUE);
define("FACT_SCORE[5]",0.7,TRUE);
?>
and then I wish to iterate over these values like this:
<?php
function grand_total()
{
$agg_score=0;
for($i=1;$i<=number_scores+1;$i++)
$agg_score=$agg_score+ (FACT_SCORE[$i])*$scores[$i];
return $agg_score;
}
?>
Well I know that this is not the right way to do it, but I can’t figure out how I attain this functionality? The configuration.php file is often changed to meet the desired requirements while many other pages use the data in it. Is there any other way of achieving this?
If you have constants with fancy names, you need to use constant() to call them:
Of course, given that it isn’t an array, it may be less confusing to get rid of square brackets:
Or simply use an array 😉