Skip to content
Advertisement

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
                        }
                     }
?>

Advertisement

Answer

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 ${}

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement