function test_loop($x, $y)
{
static $x_values = array();
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x, $y);
}
echo "gula</br>";
return $x_values;
}
function abc(){
$bababa = test_loop(1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
gula
gula
gula
gula
gula
2#3#4#5#6#
The code call test_loop() function before echo "gula", so supposedly it should fail to echo "gula". How to make the Output become like below :
gula
2#3#4#5#6#
UPDATE :
I tried to move the echo and return into else{} statement as #Joel Hager suggested, end up no output on the return value.
function test_loop($x, $y)
{
static $x_values = array();
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x, $y);
}else{
echo "gula</br>";
return $x_values;
}
}
You should add a return statement on you recursive call to test_loop
if($y < 5)
{
return test_loop($x, $y);
}
This way the code execution goes back to test_loop.
Related
I am trying to write a code that is capturing number 3. using while loop as the condition, so that the for statement will be the evaluation condition of $x, of the while loop statement, and by the same time, using if statement to evaluate the value of $x=3 and so it can echo 'three..'; . please enlighten me. thank you
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
if ($x = 3) {
echo 'three..' . "\n";
}
}
$y++;
}
In while loop, you should increment x variable.
If you increment y variable, it will always "true". So it will be endless loop.
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
// code
$x++;
}
<?php
$x = 0;
$y = 5;
while ($x <= $y) {
for ($z = 0; $z < 3; $z++) {
echo $z;
}
if ($x == 3) {
echo 'three..' . "\n";
}
$x++;
}
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
Output :
2#
How to make the output become :
2#,3#,4#
if($y < 5)
{
test_loop($x_values, $x, $y);
}
You're not doing anything with the return value of the recursive function call.
You need to add the array returned from test_loop() to the existing array:
$x_values += test_loop($x_values, $x, $y);
Your code now prints: 2#3#4#5#6#
If you want the output to be 2#, 3#, 4#, 5#, 6#, you can use the implode() function instead of a loop:
echo implode (', ', $bababa);
You'll want to utilize pass by reference:
https://www.php.net/manual/en/language.references.pass.php
I haven't looked too closely at your code, you may need to make more changes than this... but this might work:
function test_loop(&$x_values,$x, $y)
{
$x = $x + 1;
if($x < 10)
{
$x_values[] = $x."#"; // insert item 2#,3#,4# into array
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 5)
{
test_loop($x_values, $x, $y);
}
return $x_values;
}
function abc(){
$bababa = test_loop([],1,0);
foreach($bababa as $fpackage_id)
{
echo $fpackage_id;
}
}
abc();
You should use array_push() function to add an element in the array
Documentation
if($x < 10)
{
array_push($x_values,$x.'#');
}
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 4)
{
//I want to add $x value into $x_values variable, eg : $x_values = $x_values . $x;
//but $x_values = $x_values . $x; is not working, so I force to use $x_values = test_loop($x_values . $x . "##", $x, $y);
$x_values = test_loop($x_values . $x . "##", $x, $y);
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 3)
{
echo "kkk" . $y . "<br/>";
$x_values = test_loop($x_values . $x . "##", $x, $y);
}else{
echo "---------------------<br/>";
}
return $x_values;
}
function abc(){
$bababa = test_loop(0,1,0);
echo $bababa;
}
abc();
Output :
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk1
kkk2
---------------------
kkk2
---------------------
02##3##4##5##3##4##2##3##4##3##
How to make the output become :
kkk1
kkk2
---------------------
02##3##
Why don't you try to make $x_values an array, and add values in $x_values as array items, $x_values[] = $x; and then when you're ready, just implode() those values into a string. Like this:
function test_loop($x_values,$x, $y)
{
$x = $x + 1;
if($x < 4)
{
$x_values[] = $x;
}
//loop again if y is not = 3;
$y = $y + 1;
if($y < 3)
{
echo "kkk" . $y . "<br/>";
$x_values[] = $x;
}else{
echo "---------------------<br/>";
}
return implode($x_values);
}
Just make sure that you also pass $x_values as an array initially:
function abc(){
$bababa = test_loop([0],1,0);
echo $bababa;
}
This is the code I have. It currently works as is, However I'm experimenting with loops and want to see it can be done with a while loop and how it would be done. With this code I can take 2 input numbers and display them, then point out all odds, add all evens, and add all the squares of the odds.
define ("B","<br/>");
$firstNum = $_POST["firstNum"];
$secondNum = $_POST["secondNum"];
if ($firstNum < $secondNum)
{
$firstNum = true;
}
elseif ($firstNum >= $secondNum)
{
$firstNum = "You didn't listen, dumb dumb!".'<br/>GO BACK';
}
echo "First Number: ".$firstNum."<br/>"."Second Number: ".$secondNum;
echo B;
echo B;
$numbers = array();
$numbers = range($firstNum, $secondNum);
$length = count($numbers);
$odds = array();
$sumSqOdds = 0;
$sumEven = 0;
$j = 0;
for ($x = 0; $x < $length; $x++)
{
if (($numbers[$x] % 2) == 1)
{
$odds[$j] = $numbers[$x];
$sumSqOdds = $sumSqOdds + pow ($numbers[$x], 2);
$j++;
}
else
{
$sumEven = $sumEven + $numbers[$x];
}
}
$x = 0;
$y = 0;
printf("The odd numbers between your integers are: ");
for ($x = 0; $x < $j; $x++)
{
echo $odds[$x];
echo ' ';
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
echo B;
echo B;
printf("The sum of all even numbers between your integers is: ".$sumEven);
echo B;
echo B;
printf("The sum of the square of the odd numbers between your integers is: ".$sumSqOdds);
Here is my while loop but it seems to be infinite...
$numW = array ();
$numW = range ($firstNum, $secondNum);
$lengthW = count ($numW);
$oddsW = array ();
$sumSqOddsW = 0;
$sumEvenW = 0;
$j = 0;
$x = 0;
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++;
$j++;
}
else
{
$sumEvenW = $sumEvenW + $numW[$x];
}
}
$x = 0;
$y = 0;
printf ("The odd numbers between your integers are: ");
while ($x < $j)
{
$x++;
echo $oddsW[$x];
echo "nbsp;";
$y++;
if (($y % 10) == 0)
{
echo B;
}
}
Equivalent loops:
for ($i = 0; $i < 10; $i++) {
echo $i;
}
$i = 0;
while ($i < 10) {
echo $i;
$i++;
}
For a loop to ever finish it has to change one of the two evaluating variables. So either $x, or $lengthW would have to change during iteration. You made an if statment, in the first case you define that X increases by 1, but in the else case you do not change any variable that then has an effect on either $x, or $lengthW
Nor is there any check that sees if the else state has been reached and to catch that by either changing $x or $lengthW in a later iteration.
As such there's an infinite loop as soon as you reach the else case.
The if statement uses the same $x value as the last iteration checking the same position of the $numW, as such nothing has changed since the last iteration and you'll hit the else again, and again, and so on.
while ($x < $lengthW)
{
if (($numW[$x] % 2) == 1)
{
$oddsW[$j] = $numW[$x];
$sumSqOddsW = $sumSqOddsW + pow ($numW[$x], 2);
$x++; //$x is increased by one, and as such, the loop will progress.
// remove this $x++ if you place it outside the if else statement.
$j++;
}
else // reached when ($numW[$x] %2) != 1
{
$sumEvenW = $sumEvenW + $numW[$x];
// No changes to $x or $lengthW as such you'll hit the else again
// this could be solved by either adding $x++; here.
}
// or by adding $x++; here
// (if you do add it here, remove it in the if case above,
// or you risk increasing it by 2 every iteration
}
I tried and tried and tried to get this code to work and kept coming up with zilch. So I decided to try it using "for loops" instead and it worked first try. Could somebody tell me why this code is no good?
<?php
$x = $y = 10;
while ($x < 100) {
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
?>
you should reset y=10 inside the first while.
$x = 10;
while ($x < 100) {
$y = 10;
while ($y < 100) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
$y++;
}
$x++;
}
You need to reset y before the y loop begins.
While($x < 100){
$y=10; //... rest of code
For loops which loop over an integer that is incremented I would prefer the for-loop:
for ($x=0; $x < 100; $x++) {
for ($y=10; $y<100; $y++) {
$num = $x * $y;
$numstr = strval($num);
if ($numstr == strrev($numstr)) {
$pals[] = $numstr;
}
}
}
IMHO this is much more readable and it's shorter, too.