'How can I echo 'for' loop here

I want to use $i into his PHP condition how can I do this? I have no idea how to echo into condition. plz see the code and if you can plz help me. thank you advanced.

<?php

for($i=1; $i<4; $i++) {
                        if($top_h_text_**I want to use $i here!**) {
                           ?>
                           <li>
                              <?php
                              if($top_h_icon_**I want to use $i here!**) {
                                 ?>
                                 <i class="<?php echo $top_h_icon_**I want to use $i here!**;?>"></i>
                                 <?php
                              }
                              if($top_h_icon_**I want to use $i here!** == 'fa fa-envelope'){
                                 ?>
                                 <a href="mailto:<?php echo $top_h_text_**I want to use $i here!**;?>"><span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span></a>
                                 <?php
                              } else {
                                 ?>
                                 <span class="top_header_text"><?php echo $top_h_text_**I want to use $i here!**;?></span> 
                                 <?php
                              }
                              ?>
                               
                           </li>
                           <?php
                        }
                     }
?>


Solution 1:[1]

To accomplish what you are trying to do, simply follow this example:

$top_h_text_**I want to use $i here!**

becomes

${"top_h_text_".$i}

So you basically take the string you want (which should be the name of an existing variable) and wrap it with ${}

Solution 2:[2]

If this is in fact WordPress, and your top_h_icon_ and top_h_text are in fact defined. This would be appropriate method for concatenation of your $i increment and the variables. This is how you join two parts to make a single string.

Note that with WordPress and outputting dynamic variables, you should escape them.

<?php
for ( $i = 1; $i < 4; $i++ ) {
    if ( $top_h_text_ . $i ) {
        ?>
        <li>
            <?php
            if ( $top_h_icon_ . $i ) {
                ?>
            <i class="<?php echo esc_attr( $top_h_icon_ . $i ); ?>"></i>
                <?php
            }
            if ( 'fa fa-envelope' === $top_h_icon_ . $i ) {
                ?>
            <a href="mailto:<?php echo esc_attr( $top_h_text_ . $i ); ?>"><span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span></a>
                <?php
            } else {
                ?>
            <span class="top_header_text"><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
                <?php
            }
            ?>

        </li>
        <?php
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gNazi
Solution 2