So within my php file I have multiple sets of opening and closing php tags. It basically looks something like this:
<input id="myID" type="text" value="<?php
$value="x";
echo $value;
?>">
<input id="yourID" type="text" value="<?php echo $someValue; ?>"
Is there anyway to refer to $value
in the second set of php brackets? I tried using GLOBAL['index'] to refer to $value
, but I get an undefined index error.
Any help or guidance is appreciated.
Just use echo $value
.
The variable context doesn't change just because you have re-opened the PHP tag. <?php
and ?>
are just flags for the parser, and have no bearing on what your code does inside of them.
Since you're just getting started, I also recommend looking into a templating engine such as Smarty. This will help you separate application logic from your output. Also, be sure to use htmlspecialchars()
around any arbitrary data used in the context of HTML, to ensure that reserved characters are escaped, and that you aren't creating any XSS attack points.