Edward Kay wrote:
Here's the list :
<?php
function salestax($price,$tax) {
function convert_pound($dollars, $conversion=1.6) {
return $dollars * $conversion;
}
$total = $price + ($price * $tax);
echo "Total cost in dollars: $total. Cost in British pounds: "
.convert_pound($total);
}
echo convert_pound(15);
?>
I get the following error :
*Fatal error*: Call to undefined function convert_pound() in
...*Untitled-1.php* on line *18
*line 18 is this one : echo convert_pound(15);
From
http://www.daaq.net/old/php/index.php?page=php+adv+functions&parent=php+flow
+control:
"When you define a function within another function it does not exist until
the parent function is executed. Once the parent function has been executed,
the nested function is defined and as with any function, accessible from
anywhere within the current document. If you have nested functions in your
code, you can only execute the outer function once. Repeated calls will try
to redeclare the inner functions, which will generate an error."
So, as you're not calling salestax() anywhere, the convert_pound function
isn't defined.
I wasn't aware of nested functions before your question, but reading the
above on how the declaring function can only be called once, I feel they are
best avoided. Does anyone have anyone have any examples where using them
would be beneficial?
E
Ok.It was simply an oversight from my side.If I put a <br /> I get the
two results clearly.Here is the clearer code :
<?php
function salestax($price,$tax) {
function convert_pound($dollars, $conversion=1.6) {
return $dollars * $conversion;
}
$total = $price + ($price * $tax);
echo "Total cost in dollars: $total. Cost in British pounds: "
.convert_pound($total);
}
salestax(15.00,.075);
echo "<br />" . convert_pound(15);
?>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php