alamizo alamizo - 1 month ago 4x
PHP Question

How can I echo out content of if statement

i have 5 of these:

if(isset($_POST['checkbox'])) {
$name = "Alex";

How can I echo the name of every selected checkbox outside of the if statement?


From your example, build an array of $name:

if(isset($_POST['checkbox'])) {
   $name[] = "Alex";
if(isset($_POST['checkbox1'])) {
   $name[] = "Bob";

Then either:

echo implode(', ', $name);


foreach($name as $value) {
    echo $value;

But actually I'm wondering why not just set the values in the form inputs (use an array):

<input type="checkbox" name="checkbox[]" value="Alex">
<input type="checkbox" name="checkbox[]" value="Bob">


foreach($_POST['checkbox'] as $value) {
    echo $value;