You guys will be sick of me by now haha oh well. Aim: Mathematical Sum Problem: I'm pulling out the sum type e.g. + - * / but I also need to use the same variable to work out the sum Code: if($_POST['Submit']!=''){ $a = $_POST['a']; $b = $_POST['b']; $c = $_POST['c']; $d = $_POST['d']; $ansa = $_POST['ansa']; //calculations $resulta = (int)($a.$b) * (int)($c.$d); echo "Your answer was: $ansa<br>"; if ( $ansa == $resulta ) { $total = $total+1; echo "<font color=green><b>CORRECT!</b></font>"; } else { $total = $total+0; echo "<font color=red><b>INCORRECT!</b><br></font>"; echo "The correct answer was: $resulta <BR>"; } } PHP: At the moment the sum type is * so it's not random, I'm using word.txt to pull them at random, here's the form code: <form name="calc" method="post" > <table align="center" width="200" style="border:2px solid black;"> <tr> <td width="100"><font color="white" size="25px">.</font><input name="a" value="<?php echo "$random_number5";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td> <td><font color="white" size="25px">.</font><input name="b" value="<?php echo "$random_number6";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr> <tr> <td width="100"><font size="20px"><?php echo "$word";?></font><input name="c" value="<?php echo "$random_number7";?>" type="text" id="variable_a" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td> <td><font color="white" size="25px">.</font><input name="d" value="<?php echo "$random_number8";?>" type="text" id="variable_b" style="width:30px;font-size:35px;font-weight:bold;background:white;border:none;"></td></tr> <tr> <td colspan="3" align="center"><input name="ansa" type="text" id="variable_b" ></td></tr> <tr><td colspan="3" align="center"><input type="submit" name="Submit" value="Calculate" style="border:2px solid black;padding:2px;"></td></tr> </table> </form> PHP: Where the sum is being calculated I only get errors when I use: $resulta = (int)($a.$b) $word (int)($c.$d); instead of: $resulta = (int)($a.$b) * (int)($c.$d); Is there any way around this or am I just fighting a losing battle? Thanks for your help guys!
FYI, if you're trying to add $a to $b , use + , as . is used to join two strings. For example if you typed 4 for a, and 5 for b, and 6 for c, and 7 for d. Your code is basically saying 45 * 67, was that the intended behavior? You might need to do your math on one line, and join text on another.
I've changed the random number to display as 1 instead of 2. I was getting it to output the number that should be 56 as 5 and 6 instead but I've changed it to the whole number. $resulta = $a * $b; echo "<h1>$a * $b = $resulta<br></h1>"; I think I was just being stupid, I could just use an IF statement to say IF ($word == +) { $resulta = $a + $b } IF ($word == -) { $resulta = $a + $b } So on and so forth. Thanks for you advise.
Okay so I'm struggling here to. I only got errors when I fitted an if statement because I was using a variable. So I decided to test it by just outputting if it is + echo this is addition etc. Here's the code: <?php if (($word) == '+') { echo "This is an addition sum"; } if (($word) == '-') { echo "This is a subtraction sum"; } ?> PHP: The words.txt looks like so: + - The PHP will output correctly if it is a subtraction but it will not output if it's an addition very weird. Any ideas?
I've only been doing PHP for a few months and I'm teaching myself as I go, but thanks for your help here's What I came up with: <?php $sum_type = $word; ?> <?php echo "$sum_type";?> PHP: But I still need to be able to use $sum_type in my calculation here: $resulta = $a * $b; Instead of * I need to echo $sum_type there but I just get dreaded Parse error: syntax error, unexpected T_VARIABLE. Please please please somebody help meeee. Thanks.
I would suggest you to use preg_match instead of == operator and match the word and operator you are trying to compare Hope this will help you Thanks!!
Though if the word is a valid operand, he may just have to throw the entire line into an eval() function (evaluates a string as if it were a formated php line).
Is there another way I can do it. At the moment I'm storing + - / * on seperate lines in a .txt file and that's how I'm calling them by random, but the trouble is working out the sum, as the + - / * seem a lot harder than just the variable. As I said I'm not an expert I'm very much a noob so forgive me.