How to calculate factorials using recursion in PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I know this code to use in order to find the factorial of any number but what if we wanted it to do more and change up the code, so that it can calculate and print the factorials of number less that 100 recursively. How should this code be modified? Should i use for loop or some other technique?

Try this code, as suggested by LornaJane:
function factorial($number) {
if ($number < 2) {
return 1;
}
return ($number * factorial($number-1));
}

Related

convert perl function to php function (unpack, pack) [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to convert the binary representation of a number to a number in PHP. I'm currently using the following in Perl:
sub binary2decimal {
return unpack("N", pack("B32", substr("0" x 32 . shift, -32)));
}
For example, binary2decimal('1101') returns 13, and binary2decimal('1110') returns 14.
Probably you need this function: http://php.net/bindec
But i think this implementation also does the same:
function binary2decimal($param) {
return unpack("N", pack("B32",substr(str_repeat("0",32) . $param, -32)));
}

Sum up array values [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This is the array: http://www.stylechica.de/array.rtf (I can't copy it here)
I need to sum up ["PriceInformation"]["PriceInformation"]["PriceDetails"]["Price"] and allready tryed all kind of stuff like
foreach ($output["PriceInformation"]["PriceDetails"]["Price"] as $product)
echo array_sum($product);
but it just won't work. Any ideas?
Because $product refers to each iteration of $output["PriceInformation"]["PriceDetails"]["Price"], you can't sum the entire array like this. The best way to do it would be to add it to a variable as you go:
$your_sum = 0;
foreach($output as $value) {
$your_sum += $value['PriceInformation']['PriceDetails']['Price'];
}
echo 'Sum: ' . $your_sum;

Generating random numbers and setting them as variables to use later [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am wanting to generate a random number and be able to use that number through-out the whole program. At the moment I am using $random = rand($min, $max); but each time I call the variable $random it changes. Is there a way to make that constant.
All you have to do is use $random like this:
$random = rand(0,10000);
echo "$random\n";
echo "$random\n"; //same number
If you have $random inside of a function, then $random will change each time.

Using for next loop step 50 php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want steps for using for next loop.
On a vb or vb.net
for i=0 to 1000 STEP 50
....
next
How can I use this code in php?
You can use something like this:
for ($i = 0; $i <= 1000; $i += 50)
{
// Your code...
}
It is wise to read PHP for for more details.

Line break after several results from database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am pulling out data from a table, but instead of breaking a line after each result(or set of results), I would like to break the line after 'x' amount of those results, so I don't get long pages.
How can this be done with PHP?
You haven't posted any code so your question is going to get marked as off-topic.
If you're trying to do line breaks every X number of iterations then just do it.
$i = 0;
while ($row1 = mysql_fetch_assoc($result1))
{
if ($i % 5==0) {echo '<div>Multiple of 5, line break here</div>';}
$i++;
}
You have to use a pagination. Use something like this http://stefangabos.ro/php-libraries/zebra-pagination/

Categories