for loop echo inside another echo - php

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>";
}

Related

PHP wrapping numbers with for loop

I have this php code:
$count = 8;
echo '<div style="background-color:green">';
for($i=1; $i<=$count; $i++) {
echo $i;
if($i%2 == 0) {
echo '</div><div style="background-color:green">';
}
}
echo '</div>';
What this will do is to wrap the numbers in a div with green background color every 2. I wanted to make something like in the snippet
<div style="background-color:green">12</div>
<div style="background-color:yellow">34</div>
<div style="background-color:green">56</div>
<div style="background-color:yellow">78</div>
how can i do this in php? please help. Thanks in advance =)
You can do this using if else condition . suppose if $i is odd then color yellow then other green . i have added some option which give you same result.
Option 1.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
$color = $i%2 == 0 ? 'green' : 'yellow';
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
Option 2.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
if ($i%2 == 0) {
$color = 'green';
} else {
$color = 'yellow';
}
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
Option 3.
<?php
$count = 8;
for($i=1; $i<=$count; $i++) {
$color = 'yellow';
if ($i%2 == 0) {
$color = 'green';
}
echo '<div style="background-color:'.$color.'">'.$i.'</div>';
}
You could achieve that effect doing something like this:
<?php
$count = 8;
$colorMatrix = ['odd'=>'green', 'even'=>'yellow'];
for($i=1; $i<=$count; $i++) {
$oddEven = ($i%2) ? "odd" : "even";
echo "<div style='background-color:{$colorMatrix[$oddEven]}'>{$i}</div>";
}
Or even much simpler and straightforward:
$count = 8;
for($i=1; $i<=$count; $i++) {
$oddEven = ($i%2) ? "green" : "yellow";
echo "<div style='background-color:{$oddEven}'>{$i}</div>";
}
To match your snippet, start at 12 and increment 22 on each iteration. changed to DRY for my fans.
<?php
$start = 12;
$end = 78;
for ($i=$start; $i<=$end; $i=$i+22) {
echo '<div style="background-color:'.($i%4 == 0 ? 'green' : 'yellow').'">'.$i.'</div>';
}
Result:
<div style="background-color:green">12</div>
<div style="background-color:yellow">34</div>
<div style="background-color:green">56</div>
<div style="background-color:yellow">78</div>
You could also achieve the same with using a foreach with a range():
foreach (range(12, 78, 22) as $i => $num) {
echo '<div style="background-color:'.($i%2 == 0 ? 'green' : 'yellow').'">'.$num.'</div>';
}
maybe this?
$count = 8;
for($i=1; $i<=$count; $i++) {
if($i%2 == 0) {
echo '</div><div style="background-color:green">'.$i;
}else{
echo '<div style="background-color:yellow">'.$i;
}
echo '</div>'
}

PHP Triangle of Images

I am having trouble figuring out how to create a triangle using 2 sets of images. It is relatively easy to do using *. Its roughly 40 lines of code and its abit ridiculous approaching it this way. I have tried looking online but haven't found a solution
for ($i = 0; $i < 1; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($j = 0; $j < 2; $j++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 3; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 4; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 5; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 6; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 5; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 4; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 3; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 2; $i++) {
echo '<img src="Penguin.gif" style="width:50px;height:50px";/>';
}
echo '<br>';
for ($i = 0; $i < 1; $i++) {
echo '<img src="Dinosaur.gif" style="width:50px;height:50px";/>';
}
Something like this? https://repl.it/EijH/5
Replace:
* with `<img src="Dinosaur.gif" style="width:50px;height:50px";/>`and
^ with `<img src="Penguin.gif" style="width:50px;height:50px";/>`
EDIT:
<!DOCTYPE html>
<html>
<body>
<?php
$i = 0;
$j = 0;
$rows = 15;
for($i = 1; $i <= $rows; $i++) {
for($j=1; $j <= $i; ++$j) {
if($i % 2 == 0) {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>");
} else {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>");
}
}
echo "<br>";
}
for($i = $rows; $i >= 1; $i--) {
for($j=$i; $j > 1; --$j) {
echo ($j % 2 == 0 ? "<img src='http://www.k6-geometric-shapes.com/images/prism-base-square.jpg' style='width:50px;height:50px';/>" : "<img src='http://www.k6-geometric-shapes.com/images/pyramid-base-square.jpg' style='width:50px;height:50px';/>");
}
echo "<br>";
}
?>
</body>
</html>

How to do a Diamond Pattern / Shape (asterisks) inside a table? (html + php)

I have to make a diamond-shaped asterisk using for loop, inside a table. It has to have "blank" <td> spaces before and after the asterisks to move and make it look centered, so it looks like a diamond. How do I do that? (I used PHP inside an HTML code.)
Code without the <tr> and <td> tags, it looked like a diamond because it was center aligned:
<center>
<?php
echo "<table border = 1>";
// loop for the pyramid
for($i = 1; $i <= 10; $i += 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
for($j = 1; $j <= $i; $j++) {
echo "* ";
}
echo "<br />";
}
echo "</table>";
?>
</center>
Code with the <tr> and <td> tags, need "spaces" for it to look like it's center aligned:
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
for($i = 1; $i <= 10; $i += 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
for($i = 7; $i >= 1; $i -= 2) {
echo "<tr>";
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
echo "<br />";
echo "</tr>";
}
echo "</table>";
?>
Please help!
Here is new Code with your solution. I have added logic to put blank td forward and backward to *
<?php
echo "<table border = 1>";
// loop for the pyramid
echo "<tr>";
$max = $initAmount = 10;
for($i = 1; $i <= $initAmount; $i += 2) {
$max = $max -2;
$halfTD = (int)$max/2;
echo "<tr>";
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1; $b <= $halfTD; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</tr>";
// loop for the inverted pyramid, so it looks like a diamond
$max = $initAmount = 10;
for($i = 7; $i >= 1; $i -= 2) {
$max = $max -2;
$diff = $initAmount - $max;
$blankTd = $diff/2;
echo "<tr>";
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
for($j = 1; $j <= $i; $j++) {
echo "<td>* </td>";
}
for($b = 1 ; $b <= $blankTd; $b++){
echo "<td></td>";
}
echo "</tr>";
}
echo "</table>";
?>
I used the code below without using a table to make a diamond shape.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
I used the code below.
<div style="text-align: center">
<?php
$n = 8;
if($n === 1){ die("input must be greater than 1"); }
$nn = ($n * 2);
$m = (ceil($nn / 2) + 1);
$temp = 0;
for($x = 1; $x <= $nn; $x++){
$temp = (($x < $m) ? ($temp + 1) : ($temp - 1));
$total = ($temp > 1 ? ((2 * $temp) - 1) : $temp);
echo nl2br(str_repeat('* ', $total) . "\r\n");
}
?>
<?php
for($i=0;$i<=5;$i++){
for($j=5;$j>=$i;$j--){
echo ' ';
}
for($k=0;$k<=$i;$k++){
echo '*';
}
echo '<br />';
}
for($i=0;$i<=4;$i++){
for($k=0;$k<=$i+1;$k++){
echo ' ';
}
for($j=4;$j>=$i;$j--){
echo '*';
}
echo '<br />';
}
?>

I Want to print Star pattern

I want to print star pattern like :
below is my code. but it doesn't work for me. How can i fix this problem.
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
?>
You should put all text that should keep form and not whitespace trimmed in between a <pre/> tag
<pre><?php
for($i=1; $i<=5; $i++){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
}
for($i=4; $i>=1; $i--){
for($j=5; $j>$i; $j--){
echo " ";
}
echo "*";
for($j=1; $j<($i-1)*2; $j++){
echo " ";
}
if($i==1)echo "<br>";
else echo "*<br>";
} ?></pre>
Note: if your target is console and PHP-CLI, don't put the <pre/> tag, omit the end of PHP tag (?>) and replace all <br/> tags with \n.
Thanks to all of you guys. I add <pre> tag before code and also change <br> tag with \n and now it is work. Thanks again. Have a nice day guys..
Try this...
function printPattern($n)
{
$k = 0;
// Print upper triangle
for ($i = 1; $i <= $n; $i++)
{
// Print spaces
for ($j = 1; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
$k = 0;
// move to next row
echo "\n";
}
$n--;
// Print lower triangle
for ($i = $n; $i >= 1; $i--)
{
// Print spaces
for ($j = 0; $j <= $n - $i; $j++)
{
echo " ";
}
// Print #
$k = 0;
while ($k != (2 * $i - 1))
{
if ($k == 0 or $k == 2 * $i - 2)
echo "*";
else
echo " ";
$k++;
}
echo "\n";
}
}
// Driver Code
$n = 6;
printPattern($n);
// This Code is contributed by mits
?>

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?
my loop:
<?php
$i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}
?>
try below Code
<?php
for($i=1;$i<=100;$i++)
{
if($i%3==0)
{
echo $i."test"."<br>";
}
}
?>
use % operator in if loop,
i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}

Categories