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)));
}
Related
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 8 years ago.
Improve this question
i have this string
$string = "Social\Notify\Models\User";
How can i tell to php how select just the fourth segment of it? in this case just the word User?
Something like this will do?
$str='Social\Notify\Models\User';
echo explode('\\',$str)[3]; //"prints" User
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));
}
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
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;
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'm using php to execute a command to run a game server that outputs a string. I need to parse the pid out of the string so that I can save it and then use it to kill the process at a later date.
"...server daemon started with pid=6849 (parent=6848)."
So It would return 6849.
if (preg_match('/pid=(\d+)/', $string, $matches) {
$pid = $matches[1];
}