tags:

views:

93

answers:

8
$bal = (int)$balance;
echo is_int($bal);

$balance is from a file_get_contents($webservice);

I am using (int) to convert a string to integer. is_int returns 1 for true as verification.

Because the value of $bal is always negative, I do the following calculation to make it a positive:

$bal = $bal * -1;

When I do this, the value becomes 0 even though $bal may be -150 previously.

EDIT 1 var_dump ($balance) is returning: string(110) " -399.6000"

EDIT 2 I have used XML Parser to assign variable to array - see my answer below.

A: 

try this to if you want to revert the sign:

$bal = $bal * (-1);

or use abs() if you want it to be always positive:

$bal = abs($bal);
oezi
+3  A: 

I recommend PHP's abs()-function if you want to make sure that you are always dealing with a positive number:

$bal = abs($bal);
elusive
I get a return of 0 when I do this.
baswoni
The issue is that I've got bad data. var_dump ($balance) = string(110) " -399.6000"
baswoni
@baswoni: What happens if you use [`intval()`](http://php.net/intval) (probably in connection with [`trim()`](http://php.net/trim))?
elusive
oezi
@oezi: Thanks for the hint ;)
elusive
A: 
$bal = abs($bal);

http://php.net/manual/en/function.abs.php

Ruel
A: 
$balance="-150";
$bal = $balance*1;
echo $bal;

gives -150

Grumpy
... and has nothing to do with the question - thats about multiplying by -1.
oezi
A: 

hello dear i think so u can make ur calculations easy by using the function called intval() which returns the integer value of any variable or so,,,,,,,,,go through the function if u have a php manual.

Prateek
Wrikken
A: 

What is your program doing with $bal after you do the multiply? The following PHP script worked as expected on my laptop:

<?php
$balance = "-150";
$bal = (int)$balance;
echo is_int($bal);
$bal = $bal * -1;
echo $bal;
?>

Output is "1150" because I didn't bother with linebreaks. Can you post any more of your code

twon33
+2  A: 

As per your var_dump output, your string begins with a space. Use this:

$bal = intval(trim($balance));

janmoesen
More then a space, a space is not accounting for the other 100 missing characters :)
Wrikken
@Wrikken: right you are! I'm guessing his reaction was: "Oh, they don't need to see all of this junk; I'll just leave that out."
janmoesen
+1  A: 

The issue is that I was using file_get_contents to interact with a GET method for a Web Service.

This was returning XML header data which I overlooked when assigning to a variable.

To clear out the XML to leave me with just the value I wanted, I used the PHP XML_PARSER which stored file_get_contents into an array and allowed me to assign the array value to an integer.

$result = file_get_contents($base);

$p = xml_parser_create();
xml_parse_into_struct($p, $result, $vals, $index);
xml_parser_free($p);  
$bal = $vals[0]['value'];
echo $bal *-1;
baswoni