The for loop fails because "$" is an invalid variable character:
<?php
$num1 = "Number 1";
$num2 = "Number 2";
$num3 = "Number 3";
for ($i = 0; $i < 3; $i++) {
echo $num$i . "<br>";
}
?>
(I didn't understand this question)
The for loop fails because "$" is an invalid variable character:
<?php
$num1 = "Number 1";
$num2 = "Number 2";
$num3 = "Number 3";
for ($i = 0; $i < 3; $i++) {
echo $num$i . "<br>";
}
?>
(I didn't understand this question)
You're asking for variable variables. An example, like this
for ($i=1; $i<=3; $i++) {
echo ${"num".$i}."<br />";
}
Usage of variable variables can often result in messy code, so usage of an array is often considered better practice.
Should you want to try out an array, you can do it like this.
$number = array("Number 1", "Number 2", "Number 3");
You can then use a foreach-loop to echo it out, like this
foreach ($number as $value) {
echo $value."<br />";
}
or as you're using, a for-loop
for ($i=0; $i <= count($number); $i++) {
echo $number[$i]."<br />";
}
Have a look at this answer: https://stackoverflow.com/a/9257536/2911633
In your case it would be something like
for($i = 1; $i <= 3; $i++){
echo ${"num" . $i} . "<br/>";
}
But I would recommend you use arrays instead of this method as arrays give you better control.