views:

99

answers:

6

I'm using PHP to access an external API.

When a call a particular API method, it returns a number one higher than I want to display.

However, the API is returning the integer as a string and not an integer.

Question: How can I decrement the returned string by 1 since it's not an integer

Essentially, I want to do the follow pseudo-code (that doesn't work):

echo external_api() -1; // problem is, this returns "X -1" where "X" is an integer but returned as a strong
+1  A: 

Cast it with (int) or use intval() to convert it to an integer first:

echo ((int) external_api()) - 1;

or

echo intval(external_api()) - 1;

Casting is generally the fastest.

Amber
Neither work. If *external_api()* were to = 9, and I want to display 8. The code above simply displays 9 (and not 8). Weird. Any ideas?
Jason
+2  A: 

You can cast the return value as integer :

echo ((int) external_api()) - 1; 

** EDIT **

This is the code that I ran

function external_api() {
    return "100";
}

echo ((int) external_api()) - 1; 

and the output is 99. If this doesn't work for you, then the function is not returning a correct value. Otherwise, please elaborate because it doesn't make any sense.

Moreover, as deceze pointed out, even without casting, PHP is smart enough to do it already on math operators :

echo external_api() - 1; 

will also output 99 so obviously there is something going on in that mysterious function because the problem is not where you say it is.

BTW : the result of "2" -1; can never be "2 -1" because - is not even a string operator. The only string operator in PHP is . for concatenation: "2" + 1 = 3; where "2" . 1 = "21"

Yanick Rochon
Doesn't work. If *external_api()* were to = 9, and I want to display 8. The code above simply displays 9 (and not 8). Weird. Any ideas?
Jason
@Jason I can verify that the code Yanick suggested works. Perhaps your API is returning something other than a number? Perhaps a resource? Can you do a `var_dump()` on the output?
mattbasta
A: 

try echo intval(external_api())-1 or if some higher no or other data type floatval too

KoolKabin
+2  A: 

If the function is returning a string, then your code should actually work. You could try casting the value to an integer though:

echo (int)external_api() - 1;

EDIT

It sounds like you might need to sanitize your data using preg_replace before decrementing it. Try this:

echo preg_replace('/[^0-9]/', '', external_api()) - 1;
Michael
Doesn't work. If *external_api()* were to = 9, and I want to display 8. The code above simply displays 9 (and not 8). Weird. Any ideas?
Jason
Try running this at your command line (it will return 8): > php -r 'function a() { return "9"; } echo (int)a() - 1;'
Michael
Interestingly enough, for the code `echo "9NULL" - 1;`, PHP will still output 8.
mattbasta
@Michael See my second answer. Figured it out.
mattbasta
A: 

@Yanick Rochon

Assume external_api() = 9

So when I do var_dump(external_api()); it returns 9NULL

Any idea why?

Jason
Literally, the only difference between my pseudo-code and real code is that external_api() is actually called **view_count()** and view_count is defined in an external library via a web API. (So I can't view the guts/source of view_count)
Jason
@Jason At least post the full output of the command `var_dump(view_count())` then. "`9NULL`" is *not* something `var_dump` would output.
deceze
"9NULL" IS THE OUTPUT THAT var_dump is returning. It's very odd.
Jason
In retrospect, this actually is possible. See my explanation above.
mattbasta
+7  A: 

The external_api() function echoes the integer (9) and returns NULL. Thus, when you attempt to subtract from it, you get something like the following:

function external_api() {echo "9";}
echo external_api() - 1;

...which would produce 9-1.

Mystery solved.

Now, to fix it. Supposing you can't change the external api...

ob_start();
external_api();
$output = ob_get_clean();
echo ((int)$output) - 1;

Next time, though, post everything, so we're not scratching our heads for an extended period of time.

mattbasta
+1 Wow, two things to be learned: Never trust the OP (the function did not *return* anything) and (@Jason) if something is *impossible*, look for alternative explanations and test them (just calling `external_api()` without `echo` would have confirmed this).
deceze