Printing Output before getting recursion value - php

The output of the following is 1102555. How is it possible? Does recursion takes place first or echo?
abc(11);
function abc($a){
if(intval($a/2) != 0){
echo abc(intval($a/2)) + 10 * ($a/2);
}else{
echo 1;
}
}

In your case , recursion will take place first, because everytime you call the function abc with $a/2 greater than 0, the abc() in the echo gets called again and again till the value of $a/2 is less than 0.

Recursion is first in this case. Expression is evaluated from inner most to outer. So in this case it's something like this:
$a/2
intval(RESULT_OF_PREVIOUS_HERE)
abc(RESULT_OF_PREVIOUS_HERE
$a/2
10 * (RESULT_OF_PREVIOUS_HERE)
echo RESULT_OF_PREVIOUS_HERE

Related

Can't understand function and reference behaviour

I have this code.
$add = (function () {
$counter = 0;
return function () use(&$counter) {return $counter += 1;};
})();
echo $add(); //1
echo $add(); //2
echo $add(); //3
Expected Output:
111
Original Output:
123
Inside the function $counter=0 is assigned by 0 so the &$counter should be 0.
So when i called it second time it sees $counter=0 and so that &$counter will be 0, Isn't it?
Why it is incrementing?
It does not call $counter=0 for the second time. You call it just once when initiating the first function. When you call $add(), you call every time the second function (that is in your return statement) which just uses the modified value of $counter that you passed by reference. If you would add echo $counter; after the $counter = 0; you will see that.
What do you mean by "sees"? The first time you execute $add(), the inner counter is counted up. As you used a reference pointer (through adding the ampersand in use(&$counter)) to the original $counter, this is also manipulated, so after executing this once, the counter variable no longer contains a zero.
When you remove that ampersand, the innermost function uses a fresh counter each and every time, such that your expected output is met
Because you pass a reference to the function the initial $counter = 0; value is also increased each time you add 1 to it using $counter += 1; that's why your result is "123".
To get "111" you need to pass a variable to a function $counter, not a reference.

I need a program in PHP, where I search a number in a series mentioned and then give output as the index on which it is present?

I need a program in PHP, when I search a number in a given series like 1,3,7,15,31... and if it is present in the series then give output as the index on which it is present in series?
LIKE I HAVE DONE SOMETHING TO DO THIS BUT FAILED.
<?php
function test1($n) {
for($i=1;$i<=$n;$i=$c) {
$c =1 + (2 * $i);
}
}
function test2($p) {
global $c,$n;
$input=array(1);
$in=array_push($input,$c);
$k=array_search($p,$input);
$flipped = array_flip($k);
var_dump($flipped);
}
test1(1000000);
test2(45);
Like in this program I had made two functions and in FUNCTION test1 I made a formula to make the series 1,3,7,15,31,63,127.... and in FUNCTION test2 I insert a number in form of parameter and want to SEARCH that number in the series that I form above and the I want OUTPUT as the index of that number searched.
Also if the number is not present in the series then I want the output as the nearest number of the number I search.
HELP.!!!
Thank You
You've got a few problems with this code.
function test1($n) {
for($i=1;$i<=$n;$i=$c) {
$c =1 + (2 * $i);
}
}
The first problem here is that you don't do anything with $c each time you increment it. You should probably be pushing it into an array of series integers.
Secondly you don't return a result, so you can't actually use the series you would've created.
You could use something like this instead:
function test1($limit) {
$series = [];
for ($i = 1; $i <= $limit; $i = $i * 2 + 1) {
$series[] = $i;
}
return $series;
}
Next, your test2 function:
function test2($p) {
global $c,$n;
$input=array(1);
$in=array_push($input,$c);
$k=array_search($p,$input);
$flipped = array_flip($k);
var_dump($flipped);
}
Ok, first don't use global variables. Pass the ones you need in as arguments and again, return the result. To be perfectly honest, I'm not entirely sure what this function is supposed to do. All you need is the array_search call which "searches the array for a given value and returns the first corresponding key if successful".
For example:
function test2($series, $number) {
return array_search($number, $series);
}
Using these, you can do something like this:
$series = test1(1000000);
var_dump(test2($series, 45)); // bool(false)
var_dump(test2($series, 31)); // int(4)
Also if the number is not present in the series then I want the output as the nearest number of the number I search.
Ok, you'll need to write some custom logic for this. I suggest you run your array_search check, then if it returns false you loop through your series and check the following criteria:
The previous series entry is lower than your number
The next series entry is higher than your number
Then return whichever of those two has a smaller absolute difference when you subtract the series entry from your number.
I'm not going to write an example for this because it smells a bit like a school assignment, which I'm sure you're capable of doing =) good luck.

does static var get automatic 0 values PHP?

I understand the use of static inside of a function.
But what I don't understand in the next example, why the variable $x can be incremented (like it was initialized to zero):
function print_conditional() {
static $x;
if($x++ == 1) {
echo "things";
} else {
echo "good ";
}
}
print_conditional();
print_conditional();
echo PHP_EOL;
This will output "good things"
So, the first time the function is called, the variable $x with no value doesn't match in the if, but the second time, look likes it was incremented to 1 and match, how is that possible?
Decrementing NULL values has no effect, but incrementing them results in 1.
Source

Inverse factorial loop with PHP

I got bored and created this script for the sole purpose of just practicing looping. I'm trying to use a factorial number for example 479001600 which is the factorial of 12! and I'm feeding it to the loop to find what number is 479001600 a factorial of. Using the technique 479001600/2 -> 239500800/3 -> 79833600/4 ...-> previous_int/n+1 I've come up with the following code which works only till 12! but fails on 13! onwards:
<?php
function inv($int){
$j=2;
for($i=0;$i<$j;$i++){
$prod = $int/$j;
if($prod !== 1){
$int = $prod;
echo $prod. "<br>";
$j++;
} elseif($prod == 1) {
return $j;
}
}
}
echo inv(6227020800); // 13!
?>
When I try to compute the 6227020800 to 13! I get the following output:
3113510400
1037836800
259459200
51891840
8648640
1235520
154440
17160
1716
156
13
1
0.071428571428571
0.0047619047619048
0.00029761904761905
1.750700280112E-5
9.7261126672891E-7
5.1190066669943E-8
2.5595033334971E-9
1.2188111111891E-10
...etc
Even though it gets to the integer 1 through the loop division, it carries on ignoring the if statement. Is there something I'm doing wrong? Any help will be appreciated, also, I want to avoid using the gmp_ functions.
This is because 1 and 0.071428571428571 are not the same type.
Your comparison operator, !==, checks that the values are identical and since they are of different types, the check fails. To see for yourself try this:
echo gettype(1);
echo gettype(0.12);

PHP repeat a function based on return value

I was wondering how to repeat a function until a certain value is returned. I thought something like this would work but it doesn't.
list($db_Total) = getData($db_queryResult); // a random number from a mysqli query result.
function getData($db_Total){
if($db_Total){
while($arr = mysqli_fetch_array){
$db_Total = $arr['prize']; // set db_Total to database query amount
}
}
}
if($db_Total>=300){
echo "is equal or higher than 300";
while($db_Total >= 300){ // loop until you get a smaller result
list($db_Total) = getData($db_queryResult);
}
}elseif($db_Total <= 300){
echo "is lower than 300";
}
I guess the simpliest way to say it is, I would like to just run a function once, if that function returns a value higher than I don't want, I want to run that function again until it does... is that possible?
Thanks for any help.
while (($db_Total = getData($db_queryResult)) >= 300) {
echo "is equal or higher than 300";
}
echo "is lower than 300";
Here are just a few of the errors in that code:
$arr = mysqli_fetch_array should be $arr = mysqli_fetch_array($db_queryResult)
however many times you call mysqli_fetch_array it won't change the result, since you've already run the actual SQL once
or did you mean to just look at one row of a multiple-row result each time the getData function was called - in which case you don't want the while loop that's inside that function, which will consume all rows from the database result
the getData function never returns anything (no return statement)
the getData function seems to be given a DB result set, but calls it $db_Total and does nothing other than check that it's not false-y (if($db_Total))
list($foo) = bar() is used when bar() returns an array of several separate items; not sure why you think you need it here
elseif($db_Total <= 300){ should just be else
beware of infinite loops - are you absolutely guaranteed to eventually get a number lower than 300?

Categories