Functions declared inside conditional statements, such as if statements, cannot be called until the conditional statements are executed.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php if ($any_fruits) { function fruits($type) { echo "We serve $type for dinner.<br />"; } } if ($any_fruits) { fruits(grapes); } ?> </p> </body> </html> |
The condition needs to be true first to execute the function.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN"> <head> <title>PHP</title> <meta Name="Author" Content="Hann So"> </head> <body> <p> <?php $any_fruits=true; if ($any_fruits) { function fruits($type) { echo "We serve $type for dinner.<br />"; } } if ($any_fruits) { fruits(grapes); } ?> </p> </body> </html> |