This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP shift right
What is the ability to get the same result of javascript's triple great than ( >>> ) in php ?
function urshift($n, $s) {
return ($n >= 0) ? ($n >> $s) :
(($n & 0x7fffffff) >> $s) |
(0x40000000 >> ($s - 1));
}
Untested, credit goes here.
Related
This question already has answers here:
PHP FizzBuzz Logic
(3 answers)
Another FizzBuzz solution [closed]
(3 answers)
PHP Fizzbuzz Challenge [closed]
(3 answers)
PHP loop increment quirk - (FizzBuzz in one line)
(4 answers)
Closed 2 months ago.
I came across this php interview question while researching pre-interview test questions.
Given an array of integers, compute a total score based on the following rules:
Add 1 point for every even number in the array
Add 3 points for every odd number in the array
Add 5 points for every time you encounter an 8 in the array
Examples:
Input: my_numbers = [1, 2, 3, 4, 5]
Output: 11
Input: my_numbers=[15, 25, 35]
Output: 9
Input: my_numbers=[8, 8]
Output: 10
How to approach this particular task?
my attempt
<?php function find_total( $my_numbers ) {
//Insert your code here \
$total = 0;
foreach($my_numbers as $val) {
if ($val % 2 == 0) {
echo (++$val);
} else
echo ($val += 3);
}
if ($val == 8) {
echo($val += 5);}
}
?>
I am not sure your sample answers are correct (8 is an even number and should attract 1 for that as well as the 5), but to give the answer you asked for use this (PHP):
(EDITED in response to mickmackusa's and Robin Bastiaan's comments)
$total = 0;
foreach($myarray as $value) {
if ($value === 8) {
$total += 5;
} elseif (!($value % 2)) {
++$total;
} else
$total += 3;
}
This question already has answers here:
Using nested ternary operators [duplicate]
(7 answers)
Closed 3 years ago.
I've tried the shorthand but it's now working properly on me please help me to solve this problem thank you.
$acc = "free";
echo $limit = $acc == "free" ? 5 : $acc == "muyip" ? 50 : 1000;
My expected result is 5 but the result is 50
use brackets as below
$acc = "free";
echo $limit = $acc == "free" ? 5 : ($acc == "muyip" ? 50 : 1000);
This question already has answers here:
Compare floats in php
(17 answers)
Closed 6 years ago.
I'm struggling to make accounting software in PHP. And i got some bug from float precision.
Here is the sample unworking code :
$a = (float) 2258574.18;
$b = (float) 2058555.18;
$c = 200019;
$d = 0;
($b+$c-$d == $a ) ? $x = "equals" : $x = "!equals";
echo $x;
Outputs :
!equals
I was using round() abs() but doesnt solved.
It work if i only convert it to int (int).
I almost suicide because of this.
Try round() with precision 2
$e = $b+$c-$d;
(round($e, 2) == round($a, 2) ) ? $x = "equals" : $x = "!equals";
echo $x;
This question already has answers here:
The 3 different equals
(5 answers)
Closed 7 years ago.
This is what I've tried so far, but this still isn't working properly.
http://pastebin.com/w2wRaR6r
Still it's not working and I can't find why. Help please?
I've tried changing the header location method with meta refresh but that requires it to be in head, I think. Anyway thanks.
Simple - you were using a single = sign which sets a value ~ you need to use 2 to test for equality ( and missing semi-colon )
<?php
$r = rand(1, 3);
if ($r == 1) header('Location:http://www.google.com/');
if ($r == 2) header('Location:http://www.yahoo.com/');
if ($r == 3) header('Location:http://www.bing.com/');
?>
You are using = (which is assignment operator) and not == (which is comparison operator). It should be ==:
<?php
$r = rand(1, 3);
if ($r == 1)
header('Location:http://www.google.com/');
if ($r == 2)
header('Location:http://www.yahoo.com/');
if ($r == 3)
header('Location:http://www.bing.com/');
?>
This question already has answers here:
Compare floats in php
(17 answers)
Closed 9 years ago.
hello I have a problem wtich two variables in php. i have this code:
var_dump($total);echo '<br/>';
var_dump($reserva->getAdelanto());
if ($total == $reserva->getAdelanto()){
$total = 0;
echo "hello";
}
else
$total = $total - $reserva->getAdelanto();
print :
float(3940.2)
float(3940.2)
but does not enter the if when the two variables are equal. anyone knows why is that? greetings and thanks.
May be try with abs like
if ((abs($a)-abs($b)) <= 0.00001) {
echo "same";
}
Or
if (abs($a - $b) <= 0.00001) {
echo "same";
}
Or you also try like
var_dump( bccomp($a, $b) == 0 )
returns true if they are same