phpval

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 29, 2026 License: MIT Imports: 7 Imported by: 0

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

func Compare(x, y any) int

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

func Float(v any) float64

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

func GoString(v any) (string, bool)

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 Int

func Int(v any) int64

func Key added in v0.3.4

func Key(v any) any

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

func Number(v any) any

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

func NumericKey(s string) (int64, bool)

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.

func String

func String(v any) string

func Strings

func Strings(a any) []string

Strings returns a collection's values as strings in order.

func Truthy

func Truthy(v any) bool

func Values

func Values(a any) []any

Values returns a collection's values in order. A []any is returned as is: callers only read it, and the shims that do not (array_splice) copy first.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL