This question already has answers here:
Understanding slicing
(38 answers)
Closed 9 years ago.
I'm new to python, I should convert the following operation in php:
if len (s) == 88:
return s[48] + s[81:67: -1] + s[82] + s[66:62: -1] + s[85] + s[61:48 : -1] + s[67] + s[47:12: -1] + s[3] + s[11:3: -1] + s[2] + s[12]
I do not understand exactly what you mean this formatting s[a:b: -c].
s = "012345678910"
a = 7
b = 4
c = 1
print s[a:b: -c]
returns:
765
So to explain, its a slice between position 7 in the string and position 4, first element is element 0, in steps of minus one. That is to say, you get every element between 7 and 4 not including 4.
The general form of a slice as detailed in the docs is slice(start, stop[, step]).
Related
This question already has answers here:
calculate math expression from a string using eval
(10 answers)
How to mathematically evaluate a string like "2-1" to produce "1"?
(9 answers)
Closed 1 year ago.
I have + sign as string and I would like to use it as operator for 2 numbers.
Example:
$n1 = 1;
$n2 = 2;
$plus = '+';
echo $n1 . $plus . $n2;
The previous example would print 1 + 2, I want to print 3 instead.
I tried to search for a function that could convert the plus string or encode/decode it, But couldn't find a solution.
Also I tried:
echo $n1 + $plus + $n2;
But got this error: WARNING A non-numeric value encountered
Is that possible?
This question already has answers here:
Pre-incrementation vs. post-incrementation
(3 answers)
Closed 2 years ago.
php > $a = 4;
php > echo $a+++$a++;
php > 9
Why is the result equal to 9 not 8 ?
a++ will increment the value of $a, but return the original value that I held before being incremented. So $a++ return 4 and $a return 4, the result should be 8 = 4 + 4 ?
The first time you get a return value from $a++ the value will be 4. But the second time it will be 5, as you already incremented it just before:
9 = 4 + 5
The final value of $a is 6, as it has been incremented twice.
This question already has answers here:
Adding string with number in php
(6 answers)
Closed 2 years ago.
I'm a beginner in PHP. with my experience something like "number + String + number" should be a string.
But apparently, it's a number in php.
in my case:
echo 14 + ' ' + 12;
// returns 26
// expected: "14 12"
why did this happen?
and how to fix it?
You are adding a '+' for adding two numbers. These are not a string so it will do a addition.
Use this if you want to add these couple of number as a couple of string :-
echo '14'.' '.'12';
// result "14 12";
You can also use with a html code for a blank space :
echo '14 12';
// result "14 12";
Because echo accepts parameters only inside quotes. If you want text being returne use:
echo"14 12";
In you case PHP does 2 things
Takes 14 adds to 12
Concatenates space.
This question already has answers here:
Random Float between 0 and 1 in PHP
(13 answers)
Closed 4 years ago.
How do Generate a random number between 1 and 2 with a possible result of both integer or float in PHP
I try to use rand(1,2) or mt_rand(1,2) but i keep getting between 1 and 2.
I want my result so be either 1.3 , 1.1, 1.3 ,2
What about this with mt_rand() and mt_getrandmax()?
<?php
function randomFloat($min = 0, $max = 1) {
return number_format($min + mt_rand() / mt_getrandmax() * ($max - $min), 2, '.', ''); // 2 decimal places
}
echo randomFloat(1,2);
DEMO: https://3v4l.org/5a5U1
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is JavaScript's Math broken?
Adding fractions number yields different result in PHP
$grand_total = (float)$subtotal_email + (float)$delivery_email + (float)$fuel_surcharge_email - (float)$discount_coupon_email + (float)$texas_tax_email - (float)$cancel_fee_email - (float)$refund_email - (float)$refund_tax_email - (float)$coupon_tmp;
echo (float)$subtotal_email." + ".(float)$delivery_email." + ".(float)$fuel_surcharge_email." - ".(float)$discount_coupon_email." + ".(float)$texas_tax_email." - ".(float)$cancel_fee_email." - ".(float)$refund_email." - ".(float)$refund_tax_email." - ".(float)$coupon_tmp." = ".(float)$grand_total;
When I run the above in php, I get the following output:
89.99 + 0 + 16.2 - 0 + 8.61 - 3 - 100 - 10 - 1.8 = -2.88657986403E-15
But if you look at LHS, it should be 0, and this happens with or without float....any idea why?
Floating point arithmetic is never that accurate. If you need to compare to zero, you need to take the different and compare it to some small number.
if (abs($result) < 0.00001)) {
// it's zero
} else {
}
Because float. Use ints and calculate the value with cents (* 100).