There is a few issues in your code snippet.
First off: You are using a foreach-loop
, but supplying arguments as if it was a for-loop
.
You generate invalid
HTML, your id
attribute need to enclose the value in "
or '
, eg: id="para"
.
You'r <p>
tag is not closed, but instead followed by another <p>
tag (should be a </p>
).
You can use either a for-loop or a foreach-loop, but they work differently.
A for-loop would be written much like the one you got:
for($i=0;$i < count($tags); $i++) { ...
While a foreach would look more like:
foreach($tags as &$tag) { ...
Notice the &
sign before $tag
, the &
indicates that it is a reference to the object, that means, if you change the object inside the foreach loop
, it will actually be changed, not just inside the foreach
scope.
This means that you can edit the $tag
object right away instead of accessing it from the array.
If you rather access it from the array, but still wish to use a foreach
you can get both the index (the key) and the value by:
foreach($tags as $key => $value) {
$tags[$key] = 'html' . $value . 'endhtml';
}
foreach
andfor
syntax 2. Don't save html tags in an array!