0

Relevant section of current code:

$xml=simplexml_load_file($file);
$number=$xml->data->value;
imagettftext($image,$size,$angle,$x,$y,$colour,$font,$number);

The goal of this is to display a value stored in an xml file, which currently works as is. However, larger numbers are a bit of an eyesore. I figured number_format would fix this, so I tried:

$xml=simplexml_load_file($file);
$number=$xml->data->value;
imagettftext($image,$size,$angle,$x,$y,$colour,$font,number_format($number));

And:

$xml=simplexml_load_file($file);
$number=number_format($xml->data->value);
imagettftext($image,$size,$angle,$x,$y,$colour,$font,$number);

And:

$xml=simplexml_load_file($file);
$number=$xml->data->value;
$number=number_format($number);
imagettftext($image,$size,$angle,$x,$y,$colour,$font,$number);

In all three cases, no number ends up being displayed on the image I'm trying to generate. Like I said though, this works just fine without number_format. The xml file is structured like so:

<xmldata>
    <data>
        <value>123456</value>
    </data>
</xmldata>
5
  • 2
    number_format() always returns a value, in this case a NULL (and a warning if your error logging was enabled) Commented Nov 21, 2017 at 16:24
  • Try casting the value from your xml to an int or a float before calling number_format() (var_dump $xml->data->value; to see what you're actually getting back; it should be a simplexmlelement object) Commented Nov 21, 2017 at 16:24
  • var_dump results in a 500 error. Commented Nov 21, 2017 at 16:44
  • If yo get a 500 error, then you've typo'd something or created an error - DEMO Commented Nov 21, 2017 at 16:48
  • I thought as much, though I'm getting no syntax errors. I think Alvaro's answer solved the issue though. Just going to double check everything works first... Commented Nov 21, 2017 at 16:50

1 Answer 1

2

PHP isn't a black box so you can (and should) inspect your variables. You can install a debugger or dump them to screen. You also need to enable full error reporting because your code should be triggering a warning that you are not seeing:

Warning: number_format() expects parameter 1 to be double, object given

I'll jump a few steps and share a carefully composed snippet:

<?php
$xml = simplexml_load_string('<xmldata>
    <data>
        <value>123456</value>
    </data>
</xmldata>');
$value = $xml->data->value;
var_dump($value, 2*$value, number_format($value), number_format(2*$value));

Warning aside, it prints:

object(SimpleXMLElement)#4 (1) {
  [0]=>
  string(6) "123456"
}
int(246912)
NULL
string(7) "246,912"

The explanation:

  1. Most SimpleXML methods return objects that when cast to string, either implicitly or explicitly, display the corresponding node/attribute value.

  2. number_format() does not cast to string because it expects a number.

Fix:

number_format((string)$value)
Sign up to request clarification or add additional context in comments.

2 Comments

Looks like the fix worked. Regarding error reporting, normally that's easy enough to do for most uses, but in this particular case when you're generating an image, how would you view those errors? Would you output to a file?
I normally just hit Ctrl+U in the browser to see the source code.

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.