php sum with add symbol as variable - php

I have this variable
$option_value['price'] = 2
$option_value['price_prefix'] = +
$this->data['price2'] = 3
I have tried to make sum from it like this
$price = $option_value['price'].''.$option_value['price_prefix'].''.$this->data['price2'];
echo $price
but the result is 2. What I want is 5.
Prease help

Under the assumption that the prefix is always either + or -, something like this should do the trick (I've changed the variable names for the sake of readability):
$price = 2;
$prefix = "+";
$price2 = 3;
$total = $price + ($prefix.$price2);
This concatenates the prefix and the second price to "+3" which will then be cast implicitly to an integer for the addition with the first price. The parentheses make sure that the concatenation is done before the addition. Otherwise the addition would precede and that would lead to concatenation rather than addition.

You can do this like this:
<?php
$option_value['price'] = 2;
$option_value['price_prefix'] = '+';
$option_value['price2'] = 3;
$price = $option_value['price']+$option_value['price_prefix']+$option_value['price2'];
echo $price;
?>

Related

converting decimal number into long integer

I have the number 43.95
I want to convert it to the become 4395.
How do I simply do this in 1 line?
I currently have it like this:
$priceDollars = intval($priceSum);
$priceCents = $priceSum - $priceDollars;
$priceCents = round($priceCents, 2);
$priceCents = substr((string)$priceCents, 2);
print_r("$priceDollars and $priceCents");
$priceDollars=(string)$priceDollars;
$priceCents=(string)$priceCents;
$price = $priceDollars.$priceCents;
$price = (int)$price;
print_r($price);
<?php
$dec = 43.95;
echo $int = (int)str_replace('.', '', (string)$dec);//4395
a) If numbers after decimal will always be 2 then multiply by 100
<?php
$price = 43.95;
echo $price*100;
https://3v4l.org/CN9T4
b) Otherwise, you can use the #Diego De Vita code given in the comments under your question.
intval( str_replace(".", "", $pricesum) );
https://3v4l.org/dUXm2

PHP Numeric calculation e-5 e-6

When I multiply two numbers with php I get results like 3.12E-5 0.15E-6
How can I make these numbers like 0.0000010212?
Example
$amount = "100.00"; <<--- Variable
$bitcoin = "0.00000312"; <<--- Variable
$calculation = $amount * $bitcoin;
echo $calculation;
// Result : 3.12E-5
I'm a fan of the BC Math functions for things like this. In this example I've used bcmul() to multiply the values and provided 8 as the scale to support a single satoshi:
$amount = "100.00";
$bitcoin = "0.00000312";
$calculation = bcmul($amount, $bitcoin, 8);
echo $calculation; //0.00031200
$amount = floatval("100.00");
$bitcoin = floatval("0.00000312");
$calculation = $amount * $bitcoin;
$calculation = number_format($calculation,6,'.',',');
echo $calculation;

php multiplying int by decimal produces an int?

So I'm trying to understand this behaviour:
In my database table, I have a row quantity (int 11) and a column price (decimal 5,2).
Multiplying them to get a total, outputs an integer... how's this possible?
$qty = 2;
$price = 10;
$total = $qty * $price;
echo "$total";
// Outputs 20. Shouldn't it output 20,00?
So, for anyone else wondering, there's a php currency formatting function.
http://php.net/manual/it/function.money-format.php
Check the manual for further informations, thanks for the ones that answered.
You have not proven anything. 20 is the correct value.
$qty = 2;
$price = 10.23;
$total = $qty * $price;
echo "$total";
Gives 20.46, again the correct value.
If you want to format the output, use
printf("%.2f", $total);
to get results like
20.00
20.46
To adjust for locale, money_format() would be better. This would get you the , (or whatever) instead of .

Comparison Opeartor seemingly not working

I am trying to compare two values but when I do it does not appear to work. I know what the values are so it should be reporting true. Even worse, if I take either one of the variables out and put the number in it works.
$data = simplexml_load_file('xml/heroes/hero.xml')
or die("Error: Cannot create object");
$hme = $data->hes->he->maxen;
$hce = $data->hes->he->curen;
$hac = $data->hes->he->lastac;
echo $hce . ' should not be greater than ' . $hme;
if($hce > $hme){
echo 'should be working';
}
Outputs:
773 should not be greater than 20
I think your variable are like this
$hce = "773";
$hme = "20";
Before comparing them do intval
if(intval($hme)>intval($hce))
Cast your strings to integers:
$hme = (int)$data->hes->he->maxen;
$hce = (int)$data->hes->he->curen;
$hac = (int)$data->hes->he->lastac;
I think you took them as strings.I think you need to convert them to integer.
Simple function to do that:
int atoi(char *s)
{
int val = 0;
while (*s)
{
val *= 10;
val += (*s) - '0';
s++;
}
return val;
}

PHP: 3 int IDs within 1 int variable

I'm having to pass 3 variables (int) within a single numeric string called $id. To do this I'm creating $id using padding which I can then explode to get the variables. It has to be numeric otherwise I'd use underscores between the variables. I'm using eleven zeros as padding as I know the variables won't have that many zeros. So currently if I have:
$int_one = 1;
$int_two = 2;
$int_three = 3;
That would be:
$id = "1000000000002000000000003";
To create the new Id I use:
$id = $int_one . "00000000000" . $int_two . "00000000000" . $int_three;
And to separate the Id I use:
$int_one = 0;
$int_two = 0;
$int_three = 0;
if (strpos($id,"00000000000") !== false) {
$id = strrev($id); // Reversed so 0's in int's don't get counted
$id = explode("00000000000", $id);
// Set numbers back the right way
$int_one = strrev($id[2]);
$int_two = strrev($id[1]);
$int_three = strrev($id[0]);
}
This runs into problems when an individual variables is 0. Is there a way to overcome this or does it need a major rethink?
EDIT: $id is supposed to be a numeric string not int
Needs to handle int variables between 0 - 2147483647
You can just use some string magic to assure that no number has more than one zero in a row, and delimit the values using '00'. This generates a numeric string that can be uniquely decoded no matter the size or composition of the ints.
$a = 100;
$b = 0;
$c = 120;
// Encode;
$id = str_replace('0', '01', $a).'00'
.str_replace('0', '01', $b).'00'
.str_replace('0', '01', $c);
// $id = "101010001001201"
// Decode;
$tmp = split('00', $id);
$a2 = intval(str_replace('01', '0', $tmp[0]));
$b2 = intval(str_replace('01', '0', $tmp[1]));
$c2 = intval(str_replace('01', '0', $tmp[2]));
// $a2 = 100, $b2 = 0, $c2 = 120
Is there a way to overcome this or does it need a major rethink?
Yes, you'll need to rethink that. Why do you need to do it that way? Simply create a function with three parameters and pass the three ints in:
function foo($int1, $int2, $int3) {
}
Your example uses strings, not ints by the way, so you aren't even following your own requirements.
You could try this method:
$int_one = 1;
$int_two = 2;
$int_three = 3;
$id = $int_one * 1000000000000 + $int_two * 1000000 + $int_three;
// This will create a value of 1000002000003
To reverse the process:
// Get the modulo of $id / 1000000 --> 3
$int_three = $id % 1000000;
// Recalculate the base id - if you would like to retain the original id, first duplicate variable
// This would make $id = 1000002;
$id = ($id - $int_three) / 1000000;
// Again, get modulo --> 2
$int_two = $id % 1000000;
// Recalculate base id
$id = ($id - $int_two) / 1000000;
// Your first integer is the result of this division.
$int_one = $id;

Categories