Hello all and thanks in advance. I am trying to create a simple script in PHP.
Not getting the actual result of $i variable. Only hello world is printing on browser.
<?php
$d = 9;
$c = 0;
while ($d <9)
{
if ($c==0){
$i="item";
}else {
$i.=$count;
}
echo $i;
$c = $c + 1;
echo $c;
}
echo "hello world";
?>
Not able to figure out, what is missing.
Try to change your while loop like this:
$d = 0;
$c = 0;
while ($d <9)
{
if ($c==0){
$i="item";
}else {
$i.=$count;
}
echo $i;
$c = $c + 1;
echo $c;
$d++;
}
echo "hello world";
Related
I'm currently trying out PHP and I'm doing some exercises a friend told me but I can't seem to make the following:
98765
8765
765
65
5
I already made the following code:
<?php
for ($a = 9; $a >= 5; $a–-) {
for ($b = 1; $b <= $a; $b++) {
echo $a;
}
echo "<br>";
}
?>
Any tips / fixes?
Your code should be:
<?php
for($a=9; $a >= 5; $a--) {
for ($b=$a; $b >= 5; $b--) {
echo $b;
}
echo "\n";
}
<?php
function print_triangle($start, $count) {
for ($a = 0; $a < $count; $a++) {
for ($b = $start - $a; $b > $start - $count; $b--) {
echo $b;
}
echo "<br>";
}
}
print_triangle(9, 5);
?>
Hope it helps!
Well, I fixed it by creating the following code:
for ($a = 0; $a <= 9; $a++) {
for ($b = 9 - $a; $b >= 5; $b--) {
echo $b;
}
echo "<br>";
}
?>
Thanks for at least answering guys!
i tried to store variables which are set in a while loop in a multi dimensional arrays. Afterwarts i want to print the array out.
what i did:
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
}
When i run this code i will not get anything.
What is the reason for this?
Thank you!
Here is code-
<?php
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[] = array($a,$b);
$counter++;
}
/* $file_ar[1-10] = "$a","$b" */
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
echo $a.' is not '.$b;
$i++;
}
You need to add the index of the array you are adding to or you are just writing over it.
$counter = 0;
while($counter < 10){
$a = $counter + 10;
$b = $counter + 5;
$file_ar[$counter] = array($a,$b);
$counter++;
}
$i = 0;
while(isset($file_ar[$i])) {
$a = $file_ar[$i][0];
$b = $file_ar[$i][1];
if ($a != $b)
echo $a.' is not '.$b;
else
echo $a.'='.$b;
$i++;
}
How would I group 5 numbers in an array into each line? I've tried this code below but it results in something I'm not expecting it to be.
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$i]);
}
echo ("\n");
}
Result:
239298246244268
239298246244268
239298246244268
239298246244268
This loop keep repeating the first 5 numbers in my array. How do I make it to loop for every 5 numbers instead in my whole array of numbers? Thank you!
$x should be your index for the echo. Try this instead:
<?php
$result_data = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
for ($x = 0; $x < count($result_data); $x++)
{
echo ($result_data[$x]);
if(($x+1)%5==0)
{
echo ("\n");
}
}
use this $result_data[$x]
try this
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
if($x%5==0)
{
echo ("\n");
}
echo ($result_data[$x]);
}
Don't know much about your $result_data Array, but probably it should go like this:
$arrayCount = count($result_data);
for ($x = 0; $x < $arrayCount; $x++)
{
for ($i=0; $i<5; $i++)
{
echo ($result_data[$x][$i]);
}
echo ("\n");
}
I think you want to do something like this
$i = 0;
foreach($result_data as $result) {
echo $result;
if($i < 5) {
echo ",";
} else {
echo "<br/>\n";
$i = 0;
}
$i++;
}
Something like this?
$chunks = array_chunk($result_data, 5);
foreach($chunks as $chunk) {
echo implode('', $chunk);
echo "\n";
}
See http://uk3.php.net/manual/en/function.array-chunk.php
Try this several line code:
$valuesDelimiter = ', ';
$lineDelimiter = "\n";
$input_array = array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);
$slited_array = array_chunk($input_array, 5);
array_walk($slited_array, function(&$arr) {$arr = implode($valuesDelimiter, $arr);});
$result = implode($lineDelimiter, $slited_array);
I have a question: (I've already searched it on google but i couldn't find any answer).
Is it possible that the string in the variable to run first ?
For example :
<?php
function example($times) {
for($i = 4; $i < $times; $i++) echo $i;
}
$var = example(10);
echo "3$var";
?>
and this code prints :
4567893
collect result as variable inside function and return it.
<?php
function example($times) {
$result='';
for($i=4;$i<$times;$i++) $result.=$i;
return $result;
}
$var=example(10);
echo "$var"."3";
other way, only in case you can't control output of function or it have many html markups
is use output buffer capture:
<?php
function example($times) {
for($i=4;$i<$times;$i++) echo $i;
}
ob_start();
example(10);
$var=ob_get_clean;
echo "$var".3;
more info on php.net
You need to understand the difference between echo and return.
Your function example will echo output, directly. The value of $var is NULL, which displays nothing.
So what you are doing is actually the same as this:
echo 4; // in example()
echo 5; // in example()
echo 6; // in example()
echo 7; // in example()
echo 8; // in example()
echo 9; // in example()
echo "3"; // $var == '';
If you want to collect the output, write example like this:
function example($times) {
$numberstring = '';
for($i = 4; $i < $times; $i++) {
$numberstring .= $i;
}
return $numberstring;
}
Try:
function example($times) {
$str='';
for($i = 4; $i < $times; $i++)
$str .=$i;
return $str;
}
$var = example(10);
echo $var."3";
<table>
<tr
<td align="center">
<?PHP
$b = 1;
for ($a=1; $a<=1; $a++)
{
echo $b++;
echo "<br>";
}
$b = 2;
for ($a=1; $a<=2; $a++)
{
echo $b++;
}
echo "<br>";
$b = 4;
for ($a=1; $a<=3; $a++)
{
echo $b++;
}
echo "<br>";
$b = 7;
for ($a=1; $a<=5; $a++)
{
echo $b++;
{
echo "<br>";
$b = 12;
for ($a=1; $a<=5; $a++)
{
echo $b++;
}
echo "<br>";
$b = 17;
for ($a=1; $a<=4; $a++)
{
echo $b++;
}
echo "<br>";
$b = 21;
for ($a=1; $a<=3; $a++)
{
echo $b++;
}
echo "<br>";
$b = 24;
for ($a=1; $a<=2; $a++)
{
echo $b++;
}
echo "<br>";
$b = 26;
for ($a=1; $a<=1; $a++)
{
echo $b++;
}
?>
</td>
</tr>
</table>
I am trying to use for loops to make a shape that looks something along the lines of this:
I have managed to do it, but using way too many loops (9), I want to be a little less than this. Anyone have any help?
P.S its meant to be a diamond shape with the numbers 1-26
![sort of like this][1]
How about:
$arr = array(1,2,4,7,12,17,21,24,26,27);
for($i=1; $i<count($arr); $i++) {
for($j=$arr[$i-1]; $j<$arr[$i]; $j++) {
echo $j;
}
echo "<br>";
}
Something like this, just to give an idea:
$i = 0;
$leaps = array(1,2,4,7,12,17,21,24,26);
foreach($leaps as $leap){
for($j = 1; $j<=$i; $j++){
echo $leap++;
}
$i++;
}
Something like this should work:
$rowsUntilMiddle = 5;
$startnumber = 1;
$totalrows = $rowsUntilMiddle*2-1;
$i = $startnumber;
$numInRow = 0;
// each row
for ($row=1; $row<=$totalrows; $row++) {
if ($row <= $rowsUntilMiddle) {
$numInRow++;
} else {
$numInRow--;
}
// each number
for($j=0; $j<$numInRow; $j++) {
echo $i;
$i++;
}
echo '<br />';
}