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.

12
  • 3
    Also look at php.net/manual/en/function.abs.php Commented Oct 18, 2010 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. Commented Oct 18, 2010 at 12:07
  • @Jason: Also look at comment #1 ;-) Commented Oct 18, 2010 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. Commented Oct 18, 2010 at 12:13
  • 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. Commented Oct 18, 2010 at 12:20

8 Answers 8

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);
Sign up to request clarification or add additional context in comments.

4 Comments

I get a return of 0 when I do this.
The issue is that I've got bad data. var_dump ($balance) = string(110) " -399.6000"
@baswoni: What happens if you use intval() (probably in connection with trim())?
you missed the $-sign in the abs-call ;) / @baswoni: you should have noticed his fault if you don't just copy&paste...
2

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

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

2 Comments

More then a space, a space is not accounting for the other 100 missing characters :)
@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."
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;

Comments

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);

Comments

0
$bal = abs($bal);

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

Comments

0
$balance="-150";
$bal = $balance*1;
echo $bal;

gives -150

1 Comment

... and has nothing to do with the question - thats about multiplying by -1.
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

Comments

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.

1 Comment

(int)$var & intval($var) are equivalents.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.