HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpModerate

How much is 2 + 2?

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
muchhowstackoverflow

Problem

I'm currently working on a kind of PHP Quiz about mathematical calculations. We show a mathematical equation, and user need to input his answer.

Currently, this is what I have written:

echo 'Super Quizz';

echo '';

function addition($x, $y) { return $x + $y; }
function substraction($x, $y) { return $x - $y; }
function multiplication($x, $y) { return $x * $y; }
function division($x, $y) { return $x / $y; }

do {
    $firstNumber = rand(1, 10);
    $secondNumber = rand(1, 10);

    $operatorsCharacteres = array('+', '-', 'x', '/');
    $operatorsLetter = array('addition', 'substraction', 'multiplication',             'division');
    $operande = array_rand($operatorsLetter);
    $result = call_user_func_array($operatorsLetter[$operande],     array($firstNumber, $secondNumber));
}while($result ';
echo '';

echo ' ';

echo 'I valid !';
echo 'Result : '.$result;


If something is wrong, or not optimised, please, tell me, and teach me.

Solution

Naming

The naming of the arrays related to the operators is not so great:

  • In $operatorsLetter you have the names of functions that do some operation. $operatorFunctionNames or even $operatorFunctions would be a better name.



  • In $operatorsCharacteres you have the symbols of operations. These are normally not called characters. So $operatorSymbols would be a better name.



  • In $operande you have the key of a random operator. That's not an operand. In the operation 3 + 9, the operands are 3 and 9, but in your code it's "addition". So $operatorIndex would be a better name.



With these renames, the code is slightly easier to understand:

$operatorSymbols = array('+', '-', 'x', '/');
$operatorFunctions = array('addition', 'substraction', 'multiplication', 'division');
$operatorIndex = array_rand($operatorFunctions);
$operatorSymbol = $operatorSymbols[$operatorIndex];
$operatorFunction = $operatorFunctions[$operatorIndex];
$result = call_user_func_array($operatorFunction, array($firstNumber, $secondNumber));


I also introduced the helper variables $operatorSymbol and $operatorFunction,
to shorten the lines later in the code that use them.

Printing in PHP

An easier way to print in PHP is to simply move text outside of ` blocks.
For example these lines at the beginning of your script are a bit tedious to write using
echo statements:

echo 'Super Quizz';

echo '';


Instead, you could change your script to put the beginning
<?php` after those lines, like this:

Super Quizz

<?php
function addition($x, $y) { return $x + $y; }
function substraction($x, $y) { return $x - $y; }
// ...


I would do similarly near the end of the file too, like this:

// ...
echo $firstNumber.' '.$operatorSymbol.' '.$secondNumber;
?>
 = 
    ">

I valid !
Result : 

Code Snippets

$operatorSymbols = array('+', '-', 'x', '/');
$operatorFunctions = array('addition', 'substraction', 'multiplication', 'division');
$operatorIndex = array_rand($operatorFunctions);
$operatorSymbol = $operatorSymbols[$operatorIndex];
$operatorFunction = $operatorFunctions[$operatorIndex];
$result = call_user_func_array($operatorFunction, array($firstNumber, $secondNumber));
echo '<div class="table-title">Super Quizz</div>';

echo '<div class="row text-center quizz">';
<div class="table-title">Super Quizz</div>
<div class="row text-center quizz">

<?php
function addition($x, $y) { return $x + $y; }
function substraction($x, $y) { return $x - $y; }
// ...
// ...
echo $firstNumber.' '.$operatorSymbol.' '.$secondNumber;
?>
 = <input type="text" name="answer" maxlength="3" class="input-quizz" >
    <input type="hidden" value="<?php echo $result ?>">

</div>

<div class="table-title"><a href="#" type="submit">I valid !</a></div>
Result : <?php echo $result ?>

Context

StackExchange Code Review Q#79191, answer score: 10

Revisions (0)

No revisions yet.