Please see my code.
for ($row = $var; $row >= 1; --$row) {
for($j=0;$j<$row;++$j)
{echo "*";}
echo "</br>";
echo " ";
Output:
*****
****
***
**
*
But i need output as below:
*****
****
***
**
*
You might want to check out some of the string functions:
<?php
for ($i = 5; $i > 0; $i--) {
echo str_repeat(' ', 5 - $i).str_repeat('*',$i).PHP_EOL;
}
for ($i = 5; $i > 0; $i--) {
echo str_pad(str_repeat('*',$i),5,' ',STR_PAD_LEFT).PHP_EOL;
}
This runs on the command line, like so:
php filename.php
<?php
$var = 5;
echo "<div style='font-family:Courier New, Courier, monospace;'>";
for ($row = $var; $row >= 1; $row--) {
for($i=$row;$i<$var;$i++)
{
echo " ";
}
for($j=0;$j<$row;$j++)
{
echo "*";
}
echo "<br>";
}
echo "</div>";
?>
Related
I want to print number from 1 to 12 in matrix form and expected output is:
1 5 9
2 6 10
3 7 11
4 8 12
code:
<?php
for ($i=1; $i<=12; $i++)
{
for($j=1;$j<=$i;$j++)
{
echo $i." ";
}
echo "<br/>";
}
?>
I have got wrong output. So, How can I get expected output as I mention above? Please help me.
Thank You
Some "magic" code):
foreach (range(1,4) as $num) {
echo implode(' ', range($num,12,4)) . '<br />';
}
Version with for:
for ($i = 1; $i <= 4; $i++) {
for ($j = $i; $j <= 12; $j +=4) {
echo $j . ' ';
}
echo '<br />';
}
$z=0;
for ($x = 1; $x <= 4; $x++)
{
echo " $x ";
$z=$x;
for ($y = 1; $y <= 2; $y++)
{
$z=$z+4;
echo " $z ";
}
echo "\n";
}
$maxRow = 4;
$maxColumn = 3;
for ($row = 1; $row <= $maxRow; $row++)
{
for ($column = 1; $column <= $maxColumn; $column++) {
$number = $row + 4*($column-1);
echo $number." ";
}
echo "<br/>";
}
should work
foreach (range(1, 4) as $res) {
echo implode(' ', range($res, 12, 4));
echo "<br>";
}
How can I output these with a simple 'for' loop?
####*
###**
##***
#****
*****
*****
#****
##***
###**
####*
hashtag = spaces
I've tried to changing the * and the 'br' within this one:
for($i = 0; $i < 5; $i++){
for($b = 0; $b <= $i; $b++) {
echo '*';
}
echo "</br>";
}
But than it will just output them under each other >.>
for($i=1;$i<6;$i++){
for($k=0;$k<5-$i;$k++){
echo " ";
}
for($j=0;$j<$i;$j++){
echo "*";
}
echo "<br>";
}
echo "<br>";
for($i=0;$i<6;$i++){
for($j=0;$j<$i;$j++){
echo " ";
}
for($k=0;$k<5-$i;$k++){
echo "*";
}
echo "<br>";
}
My Solution. Next time you should do your homework by yourself ;-)
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
?>
I want to make in html page like this below:
But, I don't want to write them all one by one, because the number(1, 2, 3) are from $number variable and it can be more than 3.
Use a for loop:
for ($i = 1; $i <= $number; $i++) {
echo "<a href='$i'";
if ($i == $currentvid) {
echo " class='currentvid'";
}
echo "></a>";
}
Try this:
<?php
$href="1";
for($i=1;$i<4;$i++){
if($href==$i){
echo "$i";
}else{
echo "$i";
}
}
?>
$i = 1;
foreach ($number as $k)
{
if($i == 1)
{
echo "$i";
}
else
{
echo "$i";
}
}
}
This question already has answers here:
How can I create a pyramid from using php?
(14 answers)
Closed 8 years ago.
I am having problem creating a pyramid of asterisk.
Please see my code.
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
Result:
*
**
***
****
*****
My question is how I am going to make that like.
*
* *
* * *
* * * *
* * * * *
<pre><?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?></pre>
use the same program within <center> </center> tag ! like:
<center>
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
</center>
Use HTML whitespace character to procude the whitespaces:
So something like this:
<?php
// pyramid height
$height = 5;
for($i=1;$i<=$height;$i++){
for($t = 1;$t <= $height-$i;$t++)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
// use here to procude space after each asterix
echo "* ";
}
echo "<br />";
}
?>
try this
$height = 5;
$space = $height;
for($i = 1; $i <= $height; $i++) {
echo str_repeat(' ', --$space);
for($j=1;$j<=$i;$j++){
if($j > 1) {
echo ' ';
}
echo '*';
}
echo '<br />';
}
create_pyramid("*", 5);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
From link posted above by Baba
$n = 5;
$i = 0;
for($i=1; $i<=$n; $i++){
echo "<pre>";
echo str_repeat(" ", $n-$i);
echo str_repeat("# ", $i);
}