Help us shape the future of Go! Take the 2026 Go Developer Survey

phpval

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 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 AddInt added in v0.5.0

func AddInt(x, y int64) any

AddInt is PHP's + on two ints.

func Bytes added in v0.6.0

func Bytes(v any) (string, bool)

Bytes answers the string a []byte carries, and whether v was one.

A PHP string is a byte string, so a binding's []byte is one of those rather than a list of integers: regexp.Regexp.Find returns the text it matched, not fifteen numbers. This is the test the value rules ask wherever a []byte would otherwise fall through to the reflect path and read as a slice - truthiness, identity, an offset read, gettype and model.IsCollection.

func Compare

func Compare(x, y any) int

Compare is PHP 8's <=>, which sort() and rsort() order by, answering -1, 0 or 1. Its rules in order:

arrays          the shorter is smaller, an array beats any scalar
null vs string  the null becomes "" and both compare as strings
bool or null    both sides are cast to bool
numbers         numbers and numeric strings compare numerically
anything else   both sides are cast to string, compared bytewise.

func Decrement added in v0.5.0

func Decrement(v any) any

Decrement applies PHP's -- operator: numbers and numeric strings step down by one (an int64 that would overflow becomes a float). There is no string decrement; a non-numeric string is unchanged, except the empty string, which PHP reads as 0 and returns int -1 for. Null and booleans are unchanged.

func DivInt added in v0.5.0

func DivInt(x, y int64) any

DivInt divides the way PHP's / does on two ints: division by zero yields int 0 (the interpreter reports the warning elsewhere), a division that does not come out even is float, and an even one stays int.

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 Increment added in v0.5.0

func Increment(v any) any

Increment applies PHP's ++ operator: numbers and numeric strings step by one (an int64 that would overflow becomes a float, as PHP's do), null becomes int 1, and a non-numeric string takes the Perl-style alphanumeric increment. Booleans, and any type PHP only warns about, are unchanged.

func Int

func Int(v any) int64

Int reads v as PHP's (int) cast does: the leading numeric prefix of a string, zero for what has none.

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, null is "", a bool is 1 or 0, a float truncates toward zero, and a canonical decimal string is its number - "08" and "+1" are not, see NumericKey.

func ModInt added in v0.5.0

func ModInt(x, y int64) any

ModInt is PHP's %, already int-cast by the caller; modulo by zero yields int 0 to match the interpreter's phpArith.

func MulInt added in v0.5.0

func MulInt(x, y int64) any

MulInt is PHP's * on two ints.

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 OverflowAddInt added in v0.5.0

func OverflowAddInt(x, y int64) (int64, bool)

OverflowAddInt adds x and y.

func OverflowMulInt added in v0.5.0

func OverflowMulInt(x, y int64) (int64, bool)

OverflowMulInt multiplies x and y. MinInt64 * -1 is an overflow too: it has no positive counterpart.

func OverflowSubInt added in v0.5.0

func OverflowSubInt(x, y int64) (int64, bool)

OverflowSubInt subtracts y from x.

func String

func String(v any) string

String renders v the way PHP renders a value in a string context.

func StringNumber added in v0.5.0

func StringNumber(s string) (any, bool)

StringNumber returns the number a numeric string denotes, as int64 or float64 by the string's own spelling, reporting false for anything is_numeric rejects.

func Strings

func Strings(a any) []string

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

func SubInt added in v0.5.0

func SubInt(x, y int64) any

SubInt is PHP's - on two ints.

func Truthy

func Truthy(v any) bool

Truthy reads v the way `if ($v)` does: "" and "0" are false where "0.0" and "00" are true, and an empty collection is false.

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

type AutoGlobals added in v0.5.0

type AutoGlobals struct {
	Cookie  any
	Env     any
	Files   any
	Get     any
	Post    any
	Request any
	Server  any
	Session any
}

AutoGlobals holds PHP's superglobals in fixed fields instead of map entries. The set is closed by the language, so the per-store "is this a superglobal" question - asked on every variable assignment in both engines - reduces to one byte compare and a switch instead of a map probe, and a request reset is eight field writes instead of a map clear.

The fields are any, not a concrete array type: a script may assign whatever it likes over a superglobal, and phpscript keeps PHP's answer. No lock: a runtime serves one request on one goroutine, and the write path is the same single-threaded store path every other variable takes.

func (*AutoGlobals) Lookup added in v0.5.0

func (g *AutoGlobals) Lookup(name string) (any, bool)

Lookup answers a read. The bool reports a bound superglobal: an unset one answers false so the caller keeps its fallback order (globals, constants).

func (*AutoGlobals) Range added in v0.5.0

func (g *AutoGlobals) Range(visit func(name string, value any))

Range visits every bound superglobal.

func (*AutoGlobals) Reset added in v0.5.0

func (g *AutoGlobals) Reset()

Reset drops every binding; the next request's Register rebinds.

func (*AutoGlobals) Set added in v0.5.0

func (g *AutoGlobals) Set(name string, value any) bool

Set claims a superglobal store, reporting whether it did.

Jump to

Keyboard shortcuts

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