Generating random numbers and setting them as variables to use later [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 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.

Related

How to calculate factorials using recursion in 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 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));
}

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)));
}

What is the equivalent of Excel's FLOOR(number,significance) in 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 am looking to floor/round a number with a certain significance.
For example 2499 => 2400. This can be achieved in excel using =FLOOR(2499, 100)
What is the equivalent function for this in PHP? Or is there a library / custom function that solves this problem?
Use this approach:
https://stackoverflow.com/a/7491541/2731161
So:
round_down('2499',-2); #results in 2400
Note the precision value. You'll need the round_down function from the posted answer.
it's floor();
(would you believe?) there's no precision setting.. this function is from the PHP manual page - purports to add decimal places though I've not tested it.
Otherwise you could multiply by 100 (if you want 2 dps), use floor(), then divide by 100 to get the result.
Floor decimal numbers with precision:
function floor_dec($number,$precision,$separator){
$numberpart=explode($separator,$number);
$numberpart[1]=substr_replace($numberpart[1],$separator,$precision,0);
if($numberpart[0]>=0)
{$numberpart[1]=floor($numberpart[1]);}
else
{$numberpart[1]=ceil($numberpart[1]);}
$ceil_number= array($numberpart[0],$numberpart[1]);
return implode($separator,$ceil_number);
}
echo floor_dec(1.125,2,"."); //1.12
echo floor_dec(-1.3436,3,"."); //-1.344
echo floor_dec(102938.1,4,"."); //102938.1

Find missing numbers in array [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 have an php array of 99 numbers.
The array contains the digits 1 to 100 with one digit missing.
How to know the missing number?
Since you say you've found an answer yourself, here is a possible solution
$assignmentArray = array(1,2,3,4,5,6,7,8,10);
// find the missing fie... err.. missing value is actually more correct.
$missingNumber = array_sum(range(1, 10)) - array_sum($assignmentArray);
echo 'The missing number is: ' . $missingNumber;

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