1
$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.

thisisready
  • 623
  • 2
  • 10
  • 22
  • 3
    Also look at http://php.net/manual/en/function.abs.php – Tomalak Oct 18 '10 at 12:05
  • This isn't the answer, but why not use `abs()` instead of multiply be -1. Someday `$bal` may not *always* be negative. – Jason McCreary Oct 18 '10 at 12:07
  • @Jason: Also look at comment #1 ;-) – Tomalak Oct 18 '10 at 12:09
  • 1
    Although you should indeed use `abs`, there is nothing wrong with the provided code, and if `$bal==-150` & `-1==-1`, it will not give you 0. More likely, there is some garbage in `$balance` (currency signs, HTML tags, whitespace etc.) which prevents it from being cast to the desired INT. Normal handling would cast it to 0 if it cannot recognize a number. – Wrikken Oct 18 '10 at 12:13
  • Your (int) conversion is probably not doing what you think it should be doing. What does `var_dump($balance);` say? – janmoesen Oct 18 '10 at 12:14
  • I get a return of 0 when I use abs($bal) – thisisready Oct 18 '10 at 12:14
  • @baswoni: please provide a `var_dump($balance,(int)$balance);` output. – Wrikken Oct 18 '10 at 12:15
  • @janmoesen: var_dump($balance) returns: string(110) " -399.6000" – thisisready Oct 18 '10 at 12:15
  • Is there anyway to clean this up? – thisisready Oct 18 '10 at 12:16
  • 1
    @baswoni: `string(110) " -399.6000"` does not compute, if you're looking in a browser, please look at the _source_, not the webpage, as `-399.6000` has _far less_ then 110 characters. – Wrikken Oct 18 '10 at 12:20
  • I see the problem in the source. $balance is from file_get_contents ($api) and 110 chars includes the XML header data. Is there anyway to sanitize this? Source output: string(110) " -399.6000" – thisisready Oct 18 '10 at 12:25
  • I have solved the problem - please see my answer posted further below - I'm not able to tick this as the answer though - can someone do it for me? – thisisready Oct 18 '10 at 12:32

8 Answers8

3

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

$bal = abs($bal);
jwueller
  • 30,582
  • 4
  • 66
  • 70
2

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

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

janmoesen
  • 7,910
  • 1
  • 23
  • 19
  • More then a space, a space is not accounting for the other 100 missing characters :) – Wrikken Oct 18 '10 at 12:24
  • 1
    @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 Oct 18 '10 at 12:38
1

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;
thisisready
  • 623
  • 2
  • 10
  • 22
0

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
  • 51,017
  • 10
  • 98
  • 115
0
$bal = abs($bal);

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

Ruel
  • 15,438
  • 7
  • 38
  • 49
0
$balance="-150";
$bal = $balance*1;
echo $bal;

gives -150

Grumpy
  • 2,140
  • 1
  • 25
  • 38
0

I think you can make your calculations easy by using the function intval() which returns the integer value of any variable. Go through the function description if you have a php manual.

blahdiblah
  • 33,069
  • 21
  • 98
  • 152
Prateek
  • 1,538
  • 13
  • 22
0

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
  • 533
  • 2
  • 8