Can’t convert integer out of the box
Using an integer for comparing two numbers…
echo (int)$page->my_field();
That throws a notice:
Notice: Object of class Field could not be converted to int
However I can instead do this:
echo (int)$page->my_field()->value();
but I can’t find in the docs that this is a valid way to do it. I’m I supposed to add ->value()
when the plain field value is used?
That’s a limitation of PHP. Objects can only be automatically converted to strings (using the __toString()
method), not to other data types.
You can use the following though (a string can easily be converted to an int):
echo (int)(string)$page->my_field();
But yes, using ->value()
is easier to understand and the way to go.
1 Like
OMG, I didn’t even know that existed.
1 Like
I didn’t know I needed to convert numbers
I was wondering why I was getting a string
Couldn’t find the docs either