phpval

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2026 License: MIT Imports: 6 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. Every one of them coerces its arguments, and the coercion has to have 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 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 the runner stores one: an int becomes an int64, a decimal string becomes the int64 it spells, and everything else is returned unchanged. array_key_exists() and array_flip() have to name a key the same way the assignment that created it did, so this reproduces runner.normalizeKey, down to "01" being the key 1.

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 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