Loop not generating the HTML

<?php
for ($i=0; $i <4 ; $i++) {
	echo '<li class="list-group-item text-centre">'.  $features[i] . '</li>';
}
?>

What error I am doing the loop is not generating the HTML.

image

Try adding error checking instead of coding with blinkers/blinders :slight_smile:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

for ($i=0; $i <4 ; $i++) {
	echo '<li class="list-group-item text-centre">'.  $features[i] . '</li>';
}
1 Like

For the benefit of others please cut and paste the errors and/or warnings that were generated.

Also note that error_reporting and ini_set(…) Should be set in PHP.ini otherwise errors and warnings will be displayed online and could possibly include sensitive data such as password, etc

1 Like

Do you see a problem here?

4 Likes

Ok. so now it throws an error:

**Notice** : Use of undefined constant i - assumed 'i' in I ended up defining it:

Notice : Use of undefined constant i - assumed ‘i’ in /home3/…/public_html/html…com/all/stripe/index.php on line 49

PHP variables are prefixed with the dollar sign and defined constants do not require the dollar sign.

The array parameter i is missing the dollar sign.

The PHP interpreter thinks i is an undefined constant.

1 Like
$features[] = array("Feature 1", "Feature 2", "Feature 3", "Feature 4")

for ($i=0; $i <4 ; $i++) {
echo '<li class="list-group-item text-centre">'.  $features[i] . '</li>';

I am unable to understand error is still there.

Please help me get to the point sir.

Lets try it with crayons.
$features[i]
Do you see something missing here.

1 Like

Please wait. I did that before still errors we there. let me re-enforce.

Error checking in PHP is an iterative process. Jump the first hurdle, then reevaluate for the second. (IE: We can’t really help you solve problem #2 until you show us the error after you solve problem #1.)

1 Like

Fixed:

$features = array("Feature 1", "Feature 2", "Feature 3", "Feature 4")

for ($i=0; $i <4 ; $i++) {
echo '<li class="list-group-item text-centre">'.  **$features[$i]** . '</li>';

$[i] did the magic.

1 Like

I am pleased you managed to solve the problem.

Do you understand the generated error message?

2 Likes

Not fully. It was a hit and trial solution, which i know is not leading me anywhere without full comprehension of the subject. Cn you put some insight?

Read my post #6.

Let me know if you do not understand the difference between variables and defined constants.

1 Like

Yes, in general, I know that but what relevance do they have here?

In the original post the array parameter variable is missing the preceding dollar sign.

The PHP interpreter thinks the single i is a constant that has not been defined and generates the following error:

Notice : Use of undefined constant i - assumed ‘i’ in /home3/…/public_html/html…com/all/stripe/index.php on line 49

1 Like

Still confused so you want to establish that this should be:

$features[$i]
?

Try this and notice the difference:

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');

define('i', 4);

$features = array(
  "Feature 1", 
  "Feature 2", 
  "Feature 3", 
  "Feature 4",
  'DEBUG  5 '
);

  for ($i=0; $i <=4 ; $i++)
  {
  // DEBUG START
    echo 'variable $i ==>  ' .$i  .'<br>';
    echo 'constant i  ==>  ' .i .'<br>' ;
    echo '<br>'; 
  // DEBUG FINISH

    echo '<li class="list-group-item text-centre"><b>'
        . 'CONSTANT: ' 
        .   $features[i] 
        . '</b></li>'
        ;
    echo '<li class="list-group-item text-centre"><b>'
        . 'VARIABLE: ' 
        .   $features[$i] 
        . '</b></li>'
        ;

    echo '<br>'; 
  }//

Edit:

Simplified fornext loop

1 Like

I’m wondering if common convention may be partly at fault. Typically variables begin with the dollar sign, constants are UPPERCASE. So it is intuitive that it is the uppercase that signifies a constant. This is not so. (rough descr.) It is the text not being inside quotation marks.

Kind of like PHP says “This is a text character but it isn’t in quotes so it isn’t text. It isn’t a token so I’ll assume it’s a constant.”

2 Likes