Mathematical expression from string [duplicate] - php

This question already has answers here:
How to make a calculator in PHP?
(3 answers)
Closed 5 years ago.
Can someone please tell if I can in PHP evaluate math expression to number?
For example I have expresssion: (20 + 40%) + 20% I would get 33.6
I tried using EvalMath class from here: https://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html but it doesn't do the trick

Thats just simple math.
If you have an expression $x + $y% - thats the same as $x + (1 + $y/100).
In your case, you could just write
$y = 20;
$x = $y * 1.4 * 1.2;

Related

PHP numbers logic [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
What does the percent sign mean in PHP?
(6 answers)
Closed 3 years ago.
I just started to learn PHP deeper for Zend PHP certification and I found this code, which actually works. Can someone explain me the logic behind this?
<?php
$num = 20% - 8;
echo $num; // 4
What you're seeing is the modulus operator, which in essence asks "What is the remainder of 20 divided by -8".
So you might ask, why isn't it negative 4? From the manual,
The result of the modulo operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a.
See the PHP: Arithmetic Operators for more documentation.
% is not per cent, but modulus, cf. https://en.m.wikipedia.org/wiki/Modular_arithmetic
20 = 2 * 8 + 4, therefore, 20 % 8 = 4

What will be the output 012/2 in PHP? [duplicate]

This question already has answers here:
int variable with leading zero?
(5 answers)
Closed 5 years ago.
What will be the output of this expression ?
$a = 012;
$b = $a / 2;
var_dump($b);
I got the answer (int) 5 Please anyone suggest me how this is calculate ?
When you use a leading 0 in 012, you're actually using octal notation. That means, that this is 12 number but as an octal number.
So012 is actually 8+2, which is 10 as a decimal number.

How to do math with a string? [duplicate]

This question already has answers here:
How do I convert a string to a number in PHP?
(35 answers)
Closed 7 years ago.
So lets show an example here shall we?
Let's say I have 2 strings that are like this....
$math1 = "+300";
$math2 = "-125";
So obviously the answer would have to be +175 but how do I actually do the math while the input is a string.
I can't simply do
$math1 - $math2
Because it'd have to figure out if it is a + or a -, so how can this exactly be done?
You can use intval to get the integer value of a variable, and then you can do basic math operations.
<?php
$math1 = intval("+300");
$math2 = intval("-125");
echo $math1 - $math2;
?>
intval($math1) + intval($math2);
Can be:
$result = abs($math1) - abs($math2);

how to calculate php differential equation [duplicate]

This question already has answers here:
Raising to power in PHP
(5 answers)
Closed 7 years ago.
I have a calculation in php like
$outpout = 1.056^(365/10)
But it not calculating in right manner
you can try it like $outpout = (3^3);
Please help me. Thanks in advance.
As of PHP 5.6.0 we have ** operator
If you need 3^3
You can do $x = 3 ** 3 ;
If you are using older version, use
$x = pow(3, 3) ;
In both cases $x will be 27
Source: PHP Documentation

What does `<<` mean when declaring a variable in PHP? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Strange print behaviour in PHP? [duplicate]
(2 answers)
Closed 10 years ago.
I was looking at this article about facebook's HipHop Virtual Machine (HHVM) when I noticed this line:
<?php
$u_bytes =
$p_bytes = 100 << 20;
I tested it by running echo 100 << 20; and the value was 104857600. What does << 20 do?
Edit
Based on the answers it's a bitwise operator (bit shift [left]). Example:
100 = 000000000000000000001100100
^ `<< 20` moves this bit 20 bits to the left
104857600 = 110010000000000000000000000
This is a bit shift left.
You can learn more on how it works in PHP directly on PHP Manual: http://php.net/manual/en/language.operators.bitwise.php

Categories