Array of constants in php

I'm starting to do web development. My requirement here is a bit unique. I have a set of points, score_1 - score_n. Each point is associated with a factor with which it affects a grand score. Now I want to write the 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 want to iterate over these values ​​as follows:

 <?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 the wrong way to do this, but I can’t understand how I achieved this functionality? The configuration.php file is often modified to meet the desired requirements, while many other pages use the data in it. Is there any other way to achieve this?

+4
source share
6 answers

If you have constants with fancy names, you need to use constant () to call them:

 constant('FACT_SCORE[' . $i . ']') 

Of course, given that this is not an array, it may be less confusing to get rid of the square brackets:

 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); 

Or just use an array; -)

+2
source

Why not just do it in an array? The syntax is even cleaner:

 $FACT_SCORE = array( 1 => 0.2, 2 => 0.6, 3 => 0.8, 4 => 0.6, 5 => 0.7 ); 

And then just:

 function grand_total() { $agg_score=0; foreach ( $FACT_SCORE as $i => $k ) $agg_score += $scores[$i]*$k; return $agg_score; } 
+1
source
 <?php $fact_score = array(null, 0.2, 0.6, 0.8, 0.6, 0.7); 
0
source

Would define a static array for you?

 static $FACT_SCORE = array( 0.2, 0.6, 0.8, 0.6, 0.7 ); function grand_total() { $agg_score = 0; for($i = 1; $i <= number_scores + 1; $i++) $agg_score += $FACT_SCORE[$i - 1] * $scores[$i]; return $agg_score; } 
0
source

Well, I know that this is the wrong way to do this, but I can’t understand how I implement this functionality.

I would not use constants for this to be absolutely honest. A simple array is sufficient, although this would be mutable throughout the application. If you are still inclined to use constants to achieve your goal, you can use something like this:

 define("NUMBER_SCORES",5,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); function grand_total() { /** Making this up, as it not in the example. */ $scores = array( 0, 1, 2, 3, 4, 5 ); $agg_score=0; for($i=1; $i <= NUMBER_SCORES; $i++ ) { $agg_score += constant( 'FACT_SCORE[' . $i . ']' ) * $scores[$i]; } return $agg_score; } var_dump( grand_total( ) ); 

EDIT: modified and tested the example, it seems to work for me. The last example had a spelling error, and $ score was not defined.

0
source

You cannot assign arrays to constants. You could do the following:

 <?php define("FACT_SCORE", serialize(array(0.2, 0.4)), TRUE); foreach (unserialize(FACT_SCORE) as $value) { echo $value; // or calc some stuff } 

Although it is not far from the beautiful ...

0
source

All Articles