Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
how to make multiplication by 5%, for example like this.
200000 x 5% = 10000
So that's how it is?
<?php
$a = 200000;
$b = 5;
$x = $a % $b;
echo $x;
?>
Why not convert the percentage value to a decimal and multiple?
$a = 200000;
$b = 5;
$x = $a * ($b / 100);
echo $x;
The return value will be a float.
Although, I'm still somewhat unclear as to what you are actually asking.
Use normal calculation to multiplication by %
Code:
<?php
$a = 1000;
$b = 2000;
$x = $a * 100 / 100 * $b;
echo $x;
?>
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I want to store a plus sign (or any other sign) in a PHP variable and make arithmetic operation.
I tried to store sign plus in variable $c. But PHP it's not accepting it.
$a = 3;
$b = 5;
$с = "+";
But when I echo it,
echo $a. $c. $b;
it gives me the result 35.
I tried enothoer one way, but it doesn't work:
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
The result is -2.
How get result $a + $b?
You're using a combination of the Cyrillic and latin letter c in your code.
If I paste your code on this site to show each unicode char, the first c is an \u0441 (Cyrillic), how ever, the c inside the if shows 0x63 (latin), so PHP is unable to match those and trows:
PHP Notice: Undefined variable: c
Fixed:
<?php
$a = 3;
$b = 5;
$c = "+";
if ($c === "+"){
echo $a + $b;}
else {
echo $a - $b;}
Try it online!
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
When using increment on variables in Javascript, lint says that x += 1 is preferred over x ++.
But what about in PHP?
Is there any difference between the += and ++ or does it just not really matter?
Well whatever you might say about conventions, try running the following...
$i = 1; $s = 's';
$i++; $s++;
echo $i.'<br>'.$s.'<br>';
$i = 1; $s = 's';
$i += 1; $s += 1;
echo $i.'<br>'.$s.'<br>';
the output is somewhat unexpected...
2
t
2
1
so I would say it could matter very much which is chosen!
x += 1 is rather equivalent to ++x.
All those expressions (x += 1, x++ and ++x) increment the value of num by one, but the value of x++ is the value x had before it got incremented.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can anyone explain in depth why this outputs 9
here is my code in PHP:
$x = 4;
$x = $x+++$x++;
echo $x;
Execution goes like this:
$x = $x++ + $x++; ($x = 4)
$x = 4 + $x++; ($x = 5)
$x = 4 + 5; ($x = 6)
$x = 9;
For a more detailed answer of a more complex example in Java, see this answer: Incrementor logic
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
My code is:
$a = $_GET["valA"];
$b = $_GET["valB"];
$c = $_GET["valC"];
$d = pow($b,2) - 4 * $a * $c;
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
echo "x = $h";
And it is returning:
x = NAN
Please point out my mistake.
Issue is with $e = sqrt($d);
Whenever $d gives a NEGATIVE value sqrt($d) returns NAN.
EXAMPLE
echo $e = sqrt(-4);
returns NAN
AND
echo $e = sqrt(4);
returns 2
NAN simply means Not A Number. The values you are pulling from the URL may be strings, which would cause this issue.
Also I think the problem may occur if you are taking the square root of a negative number.
You could use something like this to solve it
<?php
$a = 10;
$b = 20;
$c = 30;
$d = pow($b,2) - 4 * $a * $c;
if($d < 0){
$d *= -1;
}
$e = sqrt($d);
$f = $e - $b;
$g = 2 * $a;
$h = $f / $g;
?>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Can someone try to explain me why the output of this code is 2 , 12 ; 5 , 25 ?
<?php
function swap($x, $y){
$x = $x + 1;
$y = $y + 2;
return $x * $y;
}
$a = 2;
$b = swap($a, $a);
print "$a, $b";
$b = swap(&$a, &$a);
print "$a, $b";
?>
Output:
2, 12
5, 25
Here is some explanation. The first call I think you have understood. I will explain the result of the second call in the code. Here you are passing the variable by reference.
when you call by reference the reference to the variable is passed so any change gets reflected in the original variable.
<?php
function swap($x, $y){
//here your variable $a is 2
$x = $x + 1;
// added 1 to $a so $a is now 3
$y = $y + 2;
//again added 2 to variable $a becomes 5
return $x * $y; //returns 5*5=25
}
$a = 2;
$b = swap(&$a, &$a);
print "$a, $b";
?>
Hope this explanation helps.