Documentation
¶
Overview ¶
Package phpval holds PHP's value semantics: how a value converts to a string, an integer or a truth value, how a collection reads back as a list of values or strings, and how two values order under PHP 8's <=> operator.
It exists so the files under stdlib/core can each register their own area without importing one another, and so the runner agrees with them. The coercion needs exactly one definition: sort() and in_array() disagreeing about what "10" is would be a bug no test names.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Compare ¶
Compare is PHP 8's comparison operator (<=>), which sort() and rsort() order by. Its rules, in the order they are applied:
arrays the shorter array is smaller, an array beats any scalar
null vs string the null becomes "" and the two compare as strings
bool or null both sides are cast to bool
numbers two numbers, or a number and a numeric string, or two
numeric strings, compare numerically, so "9" is less than
"10", and a list of digits out of explode() sorts the way
the same list sorts in PHP
anything else both sides are cast to string and compared bytewise, which
is where a number meeting a non-numeric string ends up
func Float ¶ added in v0.3.4
Float is PHP's float cast. It reads the same leading numeric prefix Int does, so "12abc" is 12 to both of them and "-3.5" is -3.5 here and -3 there. A string with no numeric prefix, and every value that is not a scalar, is 0.
func GoString ¶ added in v0.4.0
GoString renders a value that is none of PHP's own scalars: something a Go binding returned or a database row scanned. The final return reports whether it knew how.
A time.Time takes time.DateTime rather than Go's String or RFC3339. PHP has no string form for a date to copy: `echo $dateTime` is a fatal Error. The rule comes from where PHP writes one itself. The `date` field of var_dump, print_r and json_encode is Y-m-d H:i:s, and PDO hands back a DATETIME column as the text it was stored as. Anything else that can spell itself does, so a Duration is "1h30m0s" and a Month is "August".
func Key ¶ added in v0.3.4
Key normalises an array key the way PHP does. Only int and string keys exist; everything else converts:
null the empty string true / false 1 / 0 1.7 / -1.7 1 / -1, truncated toward zero "12" 12 "08", "+1" themselves, see NumericKey
runner.normalizeKey is this function: a key has to be stored the same way array_key_exists() and array_flip() later name it.
func Number ¶ added in v0.3.4
Number returns v in PHP's numeric domain: an int64 for what PHP treats as an integer, a float64 for what it treats as a float. It is what lets abs(), min(), max() and array_sum() hand back the type they were given, so that abs(-1) is int(1) and abs(-1.5) is float(1.5). A string is read through the same numeric prefix Int and Float read, so the three never disagree: "12abc" is int64(12), "-3.5" is float64(-3.5) and "abc" is int64(0).
func NumericKey ¶ added in v0.4.0
NumericKey reports whether s is the canonical spelling of an integer, and so becomes an int key. The string has to read back identically from the integer it would become, which rules out "08", "+1", "-0", " 1", "1.0", "1e2" and anything past int64. Each stays a string key, so $a["08"] and $a[8] are two entries. ParseInt accepts most of them, hence the hand-rolled check.
Types ¶
This section is empty.