I have a function called in the beginning of my php page and it is being called in the block of php but not further down in the html form. How far is the scope for a php function and how can I extend it to include it in the html form?
Here is an example of my code: Edit: This is NOT my actual code. It is simply showing where the scope of the function is not reaching.
<?php
function foo(){
$x = 1;
return $x;
}
if (isset($_POST['submit1']) and ! empty($_POST['submit1'])){
$x = foo();
}
?>
<div>
<form action="" method="POST">
<input type='submit' name='submit1' value='Submit'>
</form>
</div>
<div>
<form action="" method="POST">
<?php
$x = foo();
echo "<input type='text' value='".$x."'>";
?>
<input type='submit' value='Submit' name='submit2'>
</form>
</div>
The first instance of the function is called, but not the second. Can someone explain to me why. and do I need to create the function again in the second form or is there a way to extend the scope of the first function. Thank You!
Edit: This is not a function inside of a function like the linked question. This is a separate php instance where I would like to access the original function.
$x, or how you set the value$xinfoo()<?php $x = foo(); ?>to be?