Simple math with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Simple math with PHP

Post by asai »

Hi

I have a daughter struggling with simple math at school, and I want to make a web page that she can practice.
It should start with the simplest tasks like addition and subtraction.

I tried this code:

Code: Select all

<?php
 
$a = rand(0, 10); 
$b = rand(0, 10);

$answer = ($b+$a);

echo $a + $b;

<form> 
Svar: <input type="text" />
<input type="submit" />
</form>

if ( "text" == $answer ) {
	echo "The answer is correct";
} else {
	echo "The answer is wrong";
}

?>
But this don't work. :(
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Simple math with PHP

Post by AbraCadaver »

You need to name the input and then access it with GET or POST vars. You should also check to see that the form was posted so you don't get notices. Also, you probably don't want to echo the answer:

Code: Select all

$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="POST">
Svar: <input type="text" name="answer"/>
<input type="submit" name="submit" />
</form>

if ( isset($_POST['submit'] && $_POST['name'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: Simple math with PHP

Post by asai »

This code works:

Code: Select all

<?php
 
$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

echo $answer;

?>
However, when I try to put in a input field to give the anwer, the page output is blank.
This code doesn't work:

Code: Select all

<?php
 
$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="POST">
<input type="text" name="answer"/>
<input type="submit" name="submit" />
</form>

if ( isset($_POST['submit'] && $_POST['name'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}

?>
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: Simple math with PHP

Post by asai »

I want to give this code a new try...
Any suggestions on what I could try to make this work?
My daughter need to work on her math skills over the holidays... :)

EDIT: The edited script...

Code: Select all

<?php
// math.php

echo <<<_END

$a = rand(0, 10);
$b = rand(0, 10);

$answer = $a + $b;

echo $a . ' + ' . $b;

<form method="post" action="math.php">
  Answer:	<input type="text" name="a" /><br />
  <input type="submit" value="Check">
</form>

_END;

if ( isset($_POST['submit'] && $_POST['a'] == $answer ) {
        echo "The answer is correct";
} else {
        echo "The answer is wrong";
}

?>
But the page is blank... :(
Last edited by asai on Tue Dec 21, 2010 10:58 am, edited 1 time in total.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Simple math with PHP

Post by mikosiko »

Just a suggestion.... if this is not a project for you and you main goal is provide your daughter with good tools why to waste time trying to re-invent the wheel yourself? ... is a lot of good and free product in the market that she (you) can use immediately ... maybe your should consider alternatives like :
http://edubuntu.org/other-educational-systems

all of this alternatives provide a live CD that you can use in your computer without install anything... simple... quick and more efficient for your goal than develop something from scratch in with limited knowledge IMHO.
asai
Forum Commoner
Posts: 43
Joined: Tue May 04, 2010 6:24 am
Location: Norway

Re: Simple math with PHP

Post by asai »

@mikosiko
I agree with you, however she is very young, so my thought is to make this veeery simple. At first, but later when she gets better, I can try something more advanced. :)
Post Reply