Home > Back-end >  How can I echo 'for' loop here
How can I echo 'for' loop here

Time:03-20

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 ></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 ><?php echo $top_h_text_**I want to use $i here!**;?></span></a>
                                 <?php
                              } else {
                                 ?>
                                 <span ><?php echo $top_h_text_**I want to use $i here!**;?></span> 
                                 <?php
                              }
                              ?>
                               
                           </li>
                           <?php
                        }
                     }
?>

CodePudding user response:

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

CodePudding user response:

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 ></i>
                <?php
            }
            if ( 'fa fa-envelope' === $top_h_icon_ . $i ) {
                ?>
            <a href="mailto:<?php echo esc_attr( $top_h_text_ . $i ); ?>"><span ><?php echo esc_attr( $top_h_text_ . $i ); ?></span></a>
                <?php
            } else {
                ?>
            <span ><?php echo esc_attr( $top_h_text_ . $i ); ?></span>
                <?php
            }
            ?>

        </li>
        <?php
    }
}
  • Related