Pattern HoneyComb duplicating pattern - php

I am doing a pattern in php (Honey Comb pattern). My code is correct but how can I duplicate the pattern to take an argument of row and column.
Example:
Row: 2
Column: 1
The pattern will duplicate next to each other.
Row: 2
Column: 2
The pattern will duplicate next to each other and on top of each other.
Thank you
<?php
function HoneyComb(){
for($i=2; $i<=4; $i++)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
for($i=4; $i>=2; $i--)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
}
HoneyComb();
?>

Related

Duplicating or Replicating pattern for php

I would like to duplicate the pattern, the function will have 2 parameters, which is row and column. It will duplicate the pattern on how many rows and columns. My function for pattern is correct but how can I duplicate/replicate it. Should I make it in array? Thanks
<?php
$row_col_array = array();
function honey(){
$l = "";
for($i=2; $i<=4; $i++)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
for($i=4; $i>=2; $i--)
{
for($j=4; $j>=2; $j--)
{
if($i == $j)
echo "*";
else
echo " ";
}
for($k=2; $k<=4; $k++)
{
if($i == $k)
echo "*";
else
echo " ";
}
echo "<br>";
}
}
honey();
?>

How can I output this with a simple PHP for loop?

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

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
?>

How to get following output using PHP

My sample desired output should be
1 2 3 4 5
2 4
3 3
4 2
5 4 3 2 1
Here is my PHP code
for($i=1;$i <= 5;$i++) {
for($j=1;$j<=$i;$j++) {
echo "$j";
}
for($y=0;$y<(5-$i)*4;$y++) {
echo ' ';
}
for($l=$i;$l>0;$l--) {
echo "$l";
}
echo "<br/>";
}
But I got this output.
output:-
1 1
12 21
123 321
1234 4321
1234554321
Please try to solve my problem. Thanks in advance.
for($i=1; $i<=5; $i++){
echo $i." ";
}
echo "<br />";
for($i=2; $i<=5; $i++){
if($i==5){
echo $i;
}
else{
echo $i." ";
if($i==2){
echo (4)."<br />";
}
if($i==3){
echo (3)."<br />";
}
if($i==4){
echo (2)."<br />";
}
}
}
echo " ";
for($i=4; $i>=1; $i--){
echo $i." ";
}
#Mark has the best solution, I guess.
Here's a quick solution for an arbitrary array of 1-character values:
$values = range(1,7);
$count = count($values);
foreach($values as $k=>$v) {
if($k == 0)
echo implode(" ", $values), "\n";
elseif($k == $count-1)
echo implode(" ", array_reverse($values)), "\n";
else
echo $v, " ", str_repeat(" ", $count-2), $values[$count-1-$k], "\n";
}
This will produce:
1 2 3 4 5 6 7
2 6
3 5
4 4
5 3
6 2
7 6 5 4 3 2 1
$count = 5;
$last = 0;
for ($i = 1; $i <= $count; $i++) {
if($i == 1) {
for ($x = 1; $x <= 5; $x++) {
echo $x . ' ';
}
$last = $x;
} elseif ($i == 5) {
for ($b = 5; $b >= 1; $b--) {
echo $b . ' ';
}
} else{
for($c=1; $c <= 5; $c++) {
if($c == 1) {
echo $i . ' ';
} elseif ($c == 5) {
echo ($last - $i) . ' ' ;
} else {
echo ' ';
}
}
}
echo '<br>';
}

How to echoing the variable and loop?

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

Categories