Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I want to create a recursive function that displays in order n numbers from greatest to 2 that are pair. If n=5 it should display:
10 8 6 4 2
To get the pair number displayed, I am using:
if($i %2==0)
echo $i
to order from the greatest I also use i--. The problem is I don't know how this recursive function really works.
Do something like this...
<?php
$inc=2;
recv(7); // Calling your recursive function i.e. passing the parameter 5 or 7
function recv($v) // Your function definition
{
global $inc;
if($v>=1)
{
echo $inc*$v." ";
$v--;
recv($v); // Function calls itself in other words "recursive call"
}
}
OUTPUT :
14 12 10 8 6 4 2
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Can somebody explain me this code? I don't understand why it outputs 21.
<?php
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
?>
It will echo 21. I have no idea how it got this result.
The function is recursive, it calls itself until it gets to 0, then adds all the previously returned values (6,5,4,3,2,1).
function math($t){
if($t==0)
return 0;
return $t+ math($t-1);
}
echo math(6);
So on loop one it gets 6 then 6-1 = 5 so math gets called again with 5 this time and so on. Take a look at http://sandbox.onlinephpfunctions.com/code/e228f3b696c5058efee03fa978a09179c1f2ffbb.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want extract data from string
Example, i have this :
n97(nok)_100(ok)_n41(145)_23(ok)
I want get the 2 string beginning by "n" :
n97(nok) and n41(145)
And after for this 2 string i need get in variable
97 => nok and 41 => 145
can you help me ?
Thanks you
Use regex /n(\d+)\((.+?)\)/ to find patern and array_combine to get result
$str = 'n97(nok)_100(ok)_n41(145)_23(ok)';
if(preg_match_all('/n(\d+)\((.+?)\)/', $str, $m)) {
$res = array_combine($m[1], $m[2]);
}
print_r($res);
demo
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have an array like
$remarks = array('Poor','Fair','Good');
I want to access the array index by function call in it.
echo $remarks[myindex($id)];
where myindex() is a function which returns some number value from database.
How can I access the array index of my array at run time
As per you say..
myindex is a function which returns a value between 0-2 then I want to
display array value of that index
<?php
$remarks = array('Poor','Fair','Good');
function myIndex()
{
return rand(0,2);
}
echo $remarks[myIndex()]; //"prints" either Poor , Fair or Good randomly..
The myIndex() function returns a random value between 0,1 or 2 , so that is passed as the index value to your array and it prints the values either Poor , Fair or Good.
it may be
function myindex($id){
return $id % 3;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
What is the PHP function to get letter (1 char symbol), based on it position.
Like 0 position - a, 1st - b, etc.
I tried this:
"a"+5
I was expecting f but I get 5 instead.
I think you want to use chr() and/or ord() functions. Something like:
echo chr( ord("a") + $i );
$alphabet = range('a', 'z');
echo array_search('b', $alphabet); // 1
Try this chr() and/or ord() functions.
Some like:
print chr( ord("x") + $z );
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is there any way of looping through an array and return each result as it is calculated, rather than waiting until all values are calculated before they are all displayed?
Say I have an array with numbers from 1 to 10 in it, is it possible to loop through the array and return 1, then 2, then 3, then 4, then 5, etc. instead of returning 12345678910 all in one go? Obviously I have simplified this request, but basically my loop calls a function that has a 1 second timeout, so for the user to wait 10+ seconds for all results to be returned rather than a result being returned each second is far less annoying.
Yes, new PHP 5.5 provides such a thing as generators.
function one_to_ten() {
for ($i = 1; $i <= 10; $i++) {
yield $i; //It is like a return, but can be done several times.
}
}
$generator = one_to_ten(); //The loop is not executed yet. We just created the generator.
/* Some big code here */
foreach ($generator as $number){//Only here the iteration starts.
echo "{$number} <br/>" ;
}