0

I made a simple framework to deepen my understanding regarding php. In my controller, I write the below code to show view:

$varString = "test";
$varArray  = array("a", "b", "c");
include __DIR__ . './homepage.php';

The real weird thing is, both the variable is available inside the homepage.php file as I can get its value via var_dump. But as I try to loop the $varArray using foreach as below:

<? foreach($varArray as $value): ?>
<?= $value; ?>
<? endforeach; ?>

I get the following error,

Notice: Undefined variable: value

Also, adding to this confusion, there is no problem echoing $varString using echo like this:

<?= $varString; ?>

Anyone had the same issue as this before? Note that I am doing all this inside the homepage.php file.

Eddie
  • 26,593
  • 6
  • 36
  • 58
Salam.MSaif
  • 209
  • 3
  • 10

2 Answers2

1

your problem is php short tags open if you want you can use

<?php foreach($varArray as $value): ?>
<?php echo $value; ?>
<?php endforeach; ?>

or you can open php.ini short tag.

Şafak Çıplak
  • 889
  • 6
  • 12
0

This done as follows and should work

foreach($varArray as $value){
echo $value;
}

Try it as above and should work fine

Creative87
  • 125
  • 9