/    Sign up×
Community /Pin to ProfileBookmark

function not working properly

hi guys my function is below, the function works fine,
however it does not push $result onto the end of my
$answers array.

basically it should print some simple randomly generated
math problems for my son, and print the answers at the bottom
of the page.

the actual working script does add,sub(and mult and div):
[url]http://www.marketing-etcetera.com/math/2-digits.php[/url]

but i don’t want the answers to print under the problems,
i want it to add them to an $answers array and then print
the answers array in fine print along with the corresponding
problem number(i can handle this), but i can’t get the function
to push the $result onto the $answers array.

this is a scaled down version of the script and the script follows below….
[url]http://www.marketing-etcetera.com/math/test.php[/url]

by the way array_push() does work but just not from within the function.

thanks for responding!

<?

$answers = array();

function do_math(){
$pick1 = rand(0,99);
$pick2 = rand(0,99);
$result = $pick1 + $pick2;
echo “
$pick1<br>+ $pick2<br>________<br>$result
“;
array_push($answers,$result);
}

?>

<table align=”center” cellspacing=”10″>
<tr align=’right’>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
</tr>
</table>

<? print_r($answers); ?>

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarFeb 14.2008 — [code=php]<?php
$answers = array();
function do_math(){
global $answers;// add this.
$pick1 = rand(0,99);
$pick2 = rand(0,99);
$result = $pick1 + $pick2;
echo "
$pick1<br>+ $pick2<br>= ________
";
$answers[] = $result;
}

?>

<table align="center" cellspacing="10">
<tr align='right'>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
<td><? do_math(); ?></td>
</tr>
</table>

<?php
print_r($answers);
?>[/code]
or better still:
[code=php]<?php
$answers = array();
function do_math(){
$pick1 = rand(0,99);
$pick2 = rand(0,99);
$result = $pick1 + $pick2;
echo "
$pick1<br>+ $pick2<br>= ________
";
return $result;
}

?>

<table align="center" cellspacing="10">
<tr align='right'>
<td><? $answers[] = do_math(); ?></td>
<td><? $answers[] = do_math(); ?></td>
<td><? $answers[] = do_math(); ?></td>
<td><? $answers[] = do_math(); ?></td>
<td><? $answers[] = do_math(); ?></td>
</tr>
</table>

<?php
print_r($answers);
?>[/code]
Copy linkTweet thisAlerts:
@bsmbahamasauthorFeb 14.2008 — i like both solutions.

i figured it was something to do with scope

thanks
×

Success!

Help @bsmbahamas spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...