Convert plus sign string into operator [duplicate] - php

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?

Related

how to convert a string of numbers and operators to a final number in php [duplicate]

This question already has an answer here:
How to evaluate formula passed as string in PHP?
(1 answer)
Closed 2 years ago.
let's say we have a string like:
"23+sqrt(53)*7"
Is there any way to convert this to a number and print the result of it?
$string = "23+sqrt(53)*7";
$number = eval('return ' . $string . ';');
echo $number;

Any php function to to drop decimal part if its zero [duplicate]

This question already has answers here:
Remove useless zero digits from decimals in PHP
(29 answers)
Closed 6 years ago.
javascript Number function is handy when dropping undesired zero if decimal part is zero e.g
Number(2.00)
2
Is there such function in php or any alternative
I think this works that way by default in PHP. If you use proper number type like float or double.
If you're using string then you need to map
$a = '2.00';
echo (float)$a; // 2
Example of using float
$a = 2.00;
echo $a; //2
or
2.00 + 0; //2
If you want to format the number to show decimal part 2.00 you need to use number_format function (http://php.net/manual/en/function.number-format.php)
$a = 2.00
echo number_format($a, 2); // 2.00
You can achieve this by adding zero ... for example
echo "Result " . (2.00 + 0) . "\n";
Result 2
you can use floatval()
echo floatval('2.00'); //2
echo floatval('2.23'); //2.23
See the documentation for more info
you can use intval() or any roundoff function
$_float = 1.9020441;
$_float = explode(".", $_float);
echo strlen($_float[1]);`

PHP echo, how it's produce the output? [duplicate]

This question already has answers here:
Concatenation with addition in it doesn't work as expected
(2 answers)
Closed 7 years ago.
Please explain how echo understand the dot(.) with mathematical expressions and binary comma(,).
<?php
echo "The Sum: " . 2+3;
?>
//Output
3
Why 3 as output?
. and + are left-associative, so your statement is interpreted as
echo ("The Sum: " . 2) + 3;
This is equivalent to
echo "The Sum: 2" + 3;
When you add a string and a number, it converts the string to a number, which tries to find a number at the beginning of the string. Since "The Sum: 2" doesn't begin with a number, it converts to 0. So that makes the statement equivalent to
echo 0 + 3;
which simplifies to
echo 3;
and that's the result you see.
there is two operator dot(.) and plus(+) and dot has high priority so . try this
<?php
echo ("The Sum: " . 2) + 3;
?>

Unfamiliar usage of '%' in PHP script [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
I found the following line of code in a PHP script and have never seen anything like it before:
$a = ($ba%10)
What does this do?
Its is PHP's Arithmetic Operators
The result of the modulus operator % has the same sign as the dividend — that is, the result of $a % $b will have the same sign as $a. For example:
<?php
echo (5 % 3)."\n"; // prints 2
echo (5 % -3)."\n"; // prints 2
echo (-5 % 3)."\n"; // prints -2
echo (-5 % -3)."\n"; // prints -2
?>
Click PHP.NET for more information!
It tells you the remainder of a division calculation. So 25%8 would be 1. If $ba = 101 then $ba%10 would equal 1.
% is the modulus operator, it gives you the remainder of integer division.
e.g. 87 % 10 = 7

PHP math question [duplicate]

This question already has answers here:
Print numeric values to two decimal places
(6 answers)
Closed 11 months ago.
Lets say I divide 14 / 15 times it by 100 to get the percentage which is 93.33333333333333 how can I display it as 93.3% using php?
Here is the code.
$percent = ($avg / 15) * 100;
The sprintf function is made for this (see also the manual):
echo sprintf('%.1f%%', $percent);
PHP has a number_format function which lets you specify the number of decimals, what to use for the decimal separator, and what to use for the thousands separator:
$percent = ($avg / 15) * 100;
echo number_format($percent, 1) . '%'; // => 93.3%

Categories