简体   繁体   中英

for loop echo inside another echo

I have a foreach to run

for($i = 0; $i < 7; $i++){
    echo "<div class='$_Vr'">" ,$i,"</div>" <p> </p>;
}

and have a test value

if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}

I understand I can do this way to get what I want

<?php 
for($i = 0; $i < 7; $i++){
    echo "<div class='$_Vr'">" ,$i,"</div><p>"?> 

if ($_Vr == 'dd'){
echo 'yes';
}else{
echo 'no';
}

<?php </p>";  }?>

but I'd like to know if I can put another echo inside foreach ? like

for($i = 0; $i < 7; $i++){
    echo "<div class='$_Vr'">" ,$i,"</div>" <p> " , test value here , "</p>;
}

Use the conditional (AKA ternary) operator:

for($i = 0; $i < 7; $i++){
    echo "<div class='$_Vr'>$i</div> <p> " , ($_Vr == 'dd' ? "yes" : "no") , "</p>;
}
for($i = 0; $i < 7; $i++){
    echo "<div class='$_Vr'>$i</div><p>".($_Vr == 'dd' ? 'yes': 'no')."</p>";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM