Rust

value

Module

PHP value semantics (type juggling) on top of serde_json::Value.

Request parameters reach PHP code as PHP values. These functions answer the questions libraries ask about them (is_numeric, truthiness, string casts, ==) the way the engine does.

Contents

use php_std::value::Number;
Rust
pub enum Number

A PHP number after $value + 0.

use php_std::value::array_len;
Rust
pub fn array_len(value: &Value) -> usize

Number of elements of a PHP array value.

use php_std::value::array_values;
Rust
pub fn array_values(value: &Value) -> Box<dyn Iterator<Item = &Value> + '_>

Iterates the elements of a PHP array value (list or map) in order.

use php_std::value::compare_numbers;
Rust
pub fn compare_numbers(x: Number, y: Number) -> Ordering

PHP 8 <=> for two numbers.

use php_std::value::empty;
Rust
pub fn empty(value: &Value) -> bool

PHP empty($value).

use php_std::value::is_object;
Rust
pub fn is_object(value: &Value) -> bool

An empty JSON object: a stdClass instance in PHP.

use php_std::value::json_number;
Rust
pub fn json_number(n: &serde_json::Number) -> Number

A JSON number as PHP sees it: integers beyond i64 are floats.

use php_std::value::le_numbers;
Rust
pub fn le_numbers(a: Number, b: Number) -> bool

PHP 8 $a <= $b for two numbers (zend_is_smaller_or_equal): integers compare exactly; otherwise both are floats, ZEND_THREEWAY_COMPARE is not below zero for NAN, so any comparison with NAN is false.

use php_std::value::loose_eq;
Rust
pub fn loose_eq(a: &Value, b: &Value) -> bool

PHP 8 loose equality ($a == $b).

use php_std::value::loose_str_eq;
Rust
pub fn loose_str_eq(a: &[u8], b: &[u8]) -> bool

PHP $a == $b for two strings (zendi_smart_str_equals): two numeric strings compare as numbers, anything else byte for byte. Numeric strings are ASCII, so a string that is not UTF-8 only equals itself.

use php_std::value::numeric;
Rust
pub fn numeric(value: &Value) -> Option<Number>

PHP is_numeric($v) ? $v + 0 : null.

use php_std::value::numeric_bytes_ex;
Rust
pub fn numeric_bytes_ex(s: &[u8]) -> Option<(Number, i8)>

numeric_str_ex on bytes (a numeric string is ASCII, so bytes that are not UTF-8 are never numeric).

use php_std::value::numeric_str;
Rust
pub fn numeric_str(s: &str) -> Option<Number>

PHP numeric-string parsing (is_numeric + + 0).

use php_std::value::numeric_str_ex;
Rust
pub fn numeric_str_ex(s: &str) -> Option<(Number, i8)>

Like numeric_str, also reporting integer overflow as the engine's is_numeric_string_ex does: 1 or -1 when an integer-looking string did not fit in i64 and became a float.

Leading and trailing whitespace (space, \t, \n, \r, \v, \f) is allowed; the body is an optional sign and a decimal integer or float with an optional exponent. Hex, octal and binary literals are not numeric.

use php_std::value::to_string;
Rust
pub fn to_string(value: &Value) -> Option<String>

PHP (string) $value for scalars; None for arrays and objects.

use php_std::value::truthy;
Rust
pub fn truthy(value: &Value) -> bool

PHP truthiness ((bool) $value).