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 ¶
- func AddInt(x, y int64) any
- func Bytes(v any) (string, bool)
- func Compare(x, y any) int
- func Decrement(v any) any
- func DivInt(x, y int64) any
- func Float(v any) float64
- func GoString(v any) (string, bool)
- func Increment(v any) any
- func Int(v any) int64
- func Key(v any) any
- func ModInt(x, y int64) any
- func MulInt(x, y int64) any
- func Number(v any) any
- func NumericKey(s string) (int64, bool)
- func OverflowAddInt(x, y int64) (int64, bool)
- func OverflowMulInt(x, y int64) (int64, bool)
- func OverflowSubInt(x, y int64) (int64, bool)
- func String(v any) string
- func StringNumber(s string) (any, bool)
- func Strings(a any) []string
- func SubInt(x, y int64) any
- func Truthy(v any) bool
- func Values(a any) []any
- type AutoGlobals
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Bytes ¶ added in v0.6.0
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 ¶
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
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
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
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 Increment ¶ added in v0.5.0
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 ¶
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
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
ModInt is PHP's %, already int-cast by the caller; modulo by zero yields int 0 to match the interpreter's phpArith.
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.
func OverflowAddInt ¶ added in v0.5.0
OverflowAddInt adds x and y.
func OverflowMulInt ¶ added in v0.5.0
OverflowMulInt multiplies x and y. MinInt64 * -1 is an overflow too: it has no positive counterpart.
func OverflowSubInt ¶ added in v0.5.0
OverflowSubInt subtracts y from x.
func StringNumber ¶ added in v0.5.0
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.
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.