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>";}
}
Related
Please give me the solution for to print a triangle in stars pattern using php language. I used only 2 for loops to finish this.
*
***
*****
*******
Here is my code
<?php
for($a=1;$a<=7;$a++)
{
for($b=1;$b<=$a;$b++)
{
echo " *";
}
echo "<br/>";
}
echo " ";
?>
Please help me out.
<?php
for($i=0; $i < 5 ; $i++)
{
echo"<br>";
for($j=0; $j <=$i ; $j++){
echo "*" ;
}
}
?>
For more php Star patterns
This will work, still uses two for loops:
echo "<pre>";
$stars = "*";
for($a=1; $a<=4; $a++)
{
$spaces = "";
for($b = (5 - $a); $b>1; $b--)
{
$spaces .= " ";
}
echo $spaces . $stars . $spaces;
$stars .= "**";
echo "<br/>";
}
echo "</pre>";
I've used an accumulator for $stars, it starts from one star, an adds two stars at each iteration, and one accumulator for $spaces, that gets reset at each main for iteration.
The triangle is build top to bottom using one layer at a time.
One layer is made of spaces, stars and spaces again
for($x = 0; $x < $n; $x++) {
for($y = 0; $y < $n - $x; $y++) {
echo ' ';
}
for($z = 0; $z < $x * 2 +1; $z++) {
echo 'x';
}
echo "\n";
}
I run this program, but I need the output within table. So would you please solve this?
<?php
$s="*";
for($b=1; $b<=5; $b++) {
for($c=5; $c>=$b-1; $c--) {
if($c>=$b) {
echo $s;
}
else if($b != 1) {
echo " ";
}
}
for($d=5; $d>=$b; $d--) {
echo $s;
}
echo "<br/>";
}
?>
If you're fine regardless of how the code was written (only results matter), you can use this:
print('<table>');
for ($i = 0; $i < 5; $i++)
{
print('<tr>');
for ($j = 1; $j <= 5; $j++)
{
print('<td>');
(5 - $i >= $j) ? print('*') : '';
print('</td>');
}
for ($j2 = 1; $j2 <= 5; $j2++)
{
print('<td>');
(1 + $i <= $j2) ? print('*') : '';
print('</td>');
}
print('</tr>');
}
print('</table>');
What I did there is I sliced table in half vertically and used 2 for loops to fill left and right halfs. You will get something like this:
As you see I use this code to increment photos in a file, one by one :
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . '0' . $i . '.jpg"/></a></div>';
}
else
{
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . $i . '.jpg"/></a></div>';
}
}
?>
I would like to do the same but with php pages reading them from a specific directory like : ../phppages/
Any idea?
I tried but I missed something I guess and I would like to do it same way but with .php instead of .jpg.
Thank you very much.
Try this:
<?php
for ($i = $photonumber; $i >= 1; $i--) {
if($i < 10){
$i = "0".$i;
}
include("../phppages/page".$i.".php");
}
?>
Update: output by 3
<?php
$photonumber = 12;
for ($i = 0; $i < $photonumber; $i++) {
$j = $photonumber - $i;
if($j < 10){
$j = "0".$j;
}
if ($i % 3 === 0)
{
if ($i !== 0)
{
echo '</div>';
}
echo '<div>';
}
include("../phppages/page". $j.".php");
}
echo '</div>';
Update 2
To find the amount of files, that look like "page".$j.".php" in "../phppages" directory try this:
$files = glob("../phppages/page*.php");
$photonumber = count($files);
That would just be this:
for ($i = 1; $i <= $photonumber; $i++) {
include ('php-file-' . $i . '.php');
}
About having it 3 by 3, to be more precise, i want to create a fore each 3 .php file. Like
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div>';
include("../phppages/page".$i.".php") x3;
echo '</div>';
}
?>
Then it become something like this :
div : 12.php + 11.php + 10.php , div : 09.php + 08.php + 07.php , div
:
06.php + 05.php + 04.php , div : 03.php + 02.php + 01.php
I'm trying to output my array results in groups of 4.
<?php for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } ?>
The above does 4, but obviously doesn't re-loop.
You can loop whole array and group you output with help of "%" operator.
<div>
<?php for ($i = 0; $i < count($array); $i++) {
if (($i % 4) == 0) {
echo "</div><div>";
}
echo "Element " . $array[$i]; // CODE
}
</div>
Other than using Mod as the other answers show, you could use array_chunk() to create the groups:
$groups = array_chunk($original_array, 4);
foreach($groups as $group){
echo '<div>';
foreach($group as $item){
echo $item;
}
echo '</div>';
}
You can use a while loop to reloop for the whole results to be printed
<?php while(conditions) {
for ($i = 0; $i < 4; ++$i) { ?>
<div>
// code
</div>
<?php } } ?>
Try this that way you can jump by 4
for ($i = 0; $i < 20; $i = $i+4) {
echo $i.'<br/>';
}
I would use a foreach and then just throw in an extra check to output the divs.
$i=0;
foreach ($array as $key->$val)
{
if($i%3==0)
{
echo "<div>";
}
// your stuff
if($i%3==0)
{
echo "</div>";
}
$i++;
}
array_slice() returns the sequence of elements from the array array as specified by the offset and length parameters.
you can check out from here http://php.net/manual/en/function.array-slice.php
try this, use nested for loop, this will loop 4 times. You can try to integrate with your code. If
for ($i = 0; $i < 4; $i++){
for($j = 0; $j < 4; $j++){
echo $a[$j++];
}
echo "<br/>";
}
I hope it can help you.
you can try $i++, because you use ++$i in this way "for" works 3 times!
for ($i = 0; $i < 4; $i++)
I want to print integer in triangle form which look like this
1
121
12321
I tried this but I do not get the actual result
for($i=1;$i<=3;$i++)
{
for($j=3;$j>=$i;$j--)
{
echo " ";
}
for($k=1;$k<=$i;$k++)
{
echo $k;
}
if($i>1)
{
for($m=$i; $m>=1; $m--)
{
echo $m;
}
}
echo "<br>";
}
Output of this code is:
1
1221
123321
Where am I going wrong, please guide me.
Another integer solution:
$n = 9;
print str_pad ("✭",$n," ",STR_PAD_LEFT) . PHP_EOL;
for ($i=0; $i<$n; $i++){
print str_pad ("", $n - $i);
for ($ii=-$i; $ii<=$i; $ii++){
if ($i % 2 != 0 && $ii % 2 == 0)
print "&#" . rand(10025,10059) . ";";
else print $i - abs($ii) + 1;
}
print PHP_EOL;
}
✭
1
1✬1
12321
1❊3✪3✳1
123454321
1✼3✶5❃5❈3✸1
1234567654321
1✾3✯5✿7❉7✫5✷3✶1
12345678987654321
Or if you already have the string, you could do:
$n = 9; $s = "12345678987654321"; $i = 1;
while ($i <= $n)
echo str_pad ("", $n-$i) . substr ($s,0,$i - 1) . substr ($s,-$i++) . PHP_EOL;
Your code should be this:
for($i=1;$i<=3;$i++)
{
for($j=3;$j>$i;$j--)
{
echo " ";
}
for($k=1;$k<$i;$k++) /** removed = sign*/
{
echo $k;
}
if($i>=1) /**added = sign*/
{
for($m=$i; $m>=1; $m--)
{
echo $m;
}
}
echo "<br>";
}
Try this.
Details:
Your loop is not proper as in case of for($k=1;$k<=$i;$k++), this will print the
repeated number when check the condition for less then and again for equals to.
So remove the equals sign.
reason to add the eqaul sign in if($i>=1) is that the first element will not print if there will not be equals as first it will be print by for loop from where removed the equal sign.
Your output will be this:
1
121
12321
For all the x-mas lovers:
$max = 9; # can be 2 .. 9
for($i = 1; $i <= $max; $i++) {
$line = (str_pad('', $max - $i));
for($ii = 1; $ii <= $i; $ii++) {
$line .= $ii;
}
for($ii = $i-1; $ii > 0; $ii--) {
$line .= $ii;
}
echo $line . PHP_EOL;
}
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
Amazing what computers are able to achieve nowadays! Isn't it?
A little late to the party, but here's yet another solution that uses a "for" loop with two initialization variables and a ternary-based incrementer/decrementer. It's an unorthodox use of a "for" loop, but it's still perfectly valid and arguably makes the code more elegant and easier to follow. I chose to add space before and after each semicolon and omit all other space inside the parentheses so it's easier to visualize each of the three pieces of the "for" loop (initialization, condition, increment/decrement):
$count = 9;
echo "<pre>";
for ($i=1; $i<=$count; $i++) {
echo str_pad("",$count-$i," ",STR_PAD_LEFT);
for ( $j=1,$up=true ; $j>0 ; $up?$j++:$j-- ) {
echo $j;
if ($j==$i) {$up = false;}
}
echo "<br>";
}
echo "</pre>";
Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321