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();
?>
Related
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();
?>
I try to make a function that creates some triangles.
<pre>
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
for ($j=1;$j<=$max;$j++){
if ($i = $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
echo ""."<br>";
}
}
?>
</pre>
The line of for ($i=1; $i<=$angka; $i++) is stop. It just run it once.
The next $i is not running. There is no next $i.
Actually, I want to make it as a function it, but this still doesn't work.
It looks like this may be the culprit
if ($i = $j){
It should be
if ($i == $j){
Because you're setting $i to $j which if $angka is 1, it will exit the for loop since it's $i <=$angka
Here is the fixed code:
<?php
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
///*
for ($j=1;$j<=$max;$j++){
if ($i == $j){ //NOTICE how this needs to be `==` and not `=`
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
//$segitiga = $segitiga . ($ctr * $ctr);
echo " ". $ctr * $ctr;
}else{
//$segitiga = $segitiga . " ";
echo " ";
}
}
//*/
//$segitiga = $segitiga . "\n";
echo ""."<br>";
}
//echo $segitiga;
}
?>
for ($i=1; $i<=$angka; $i++){
your $i start at 1 and stops where it is equal to $angka
where $angka could be 0 because of
$angka = isset($_POST['angka']) ? $_POST['angka'] : "0";
so that loop may never execute , not even once
if you want to test what I'm saying, I'm executing your code with $angka set manually and it runs normally
here is the output for the following code
$angka = "3";
echo "<pre>";
if ($angka)
{
$segitiga = "";
$max = $angka + $angka - 1;
$ctr = 0;
for ($i=1; $i<=$angka; $i++){
echo $i;
echo 'the i loop here'.PHP_EOL;
for ($j=1;$j<=$max;$j++){
echo 'the j loop here'.PHP_EOL;
if ($i == $j){
if($j<=$angka){
$ctr++;
}elseif($j>$angka){
$ctr--;
}
echo " ". $ctr * $ctr;
}else{
echo " ";
}
}
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";
}
}
}