intval / floatval / strval / boolval
intval(mixed $value, int $base = 10): int — floatval(mixed $value): float — strval(mixed $value): string — boolval(mixed $value): bool
The four explicit conversion functions. Trap table: intval('123abc')=123 (prefix parsing), boolval('0')=false (the only falsy string), all equivalent to the (int)/(bool) operators.
C C23
#include <stdio.h>
#include <stdlib.h>
int main(void) {
/* strtol parses the numeric prefix - the intval("123abc") behavior */
printf("%ld\n", strtol("123abc", NULL, 10)); /* 123 */
printf("%d\n", (int) 1.9); /* cast truncates toward zero */
printf("%d\n", (int) ('x' != 0)); /* any nonzero byte is true, like PHP */
return 0;
}
123 1 1
C++ C++26
#include <iostream>
#include <string>
int main() {
// std::stoi also parses the numeric prefix, like intval
std::cout << std::stoi("123abc") << "\n"; // 123
std::cout << (int) 1.9 << "\n"; // 1
// std::stoi("abc") throws std::invalid_argument instead of returning 0
return 0;
}
123 1
C# v11.0.100
Console.WriteLine(int.Parse("42")); // strict: the whole string must parse
var ok = int.TryParse("123abc", out var _); // False: no prefix parsing
Console.WriteLine(ok);
// bool.Parse only accepts "True"/"False": "0" throws FormatException,
// stricter than PHP's boolval("0") == false
try {
Convert.ToBoolean("0");
} catch (FormatException) {
Console.WriteLine("FormatException");
}
42 False FormatException
Go v1.27.4
package main
import (
"fmt"
"strconv"
)
func main() {
i, err := strconv.Atoi("42")
fmt.Println(i, err == nil) // 42 true
_, err = strconv.Atoi("123abc")
fmt.Println(err != nil) // true: strict, no prefix parsing
f, _ := strconv.ParseFloat("3.14", 64)
fmt.Println(f == 3.14)
b, _ := strconv.ParseBool("0")
fmt.Println(b) // false: Go also accepts "0" as false
}
42 true true true false
Java v27.0.1
public class Main {
public static void main(String[] args) {
System.out.println(Integer.parseInt("42")); // strict: whole string only
try {
Integer.parseInt("123abc"); // no prefix parsing - throws
} catch (NumberFormatException e) {
System.out.println("NumberFormatException");
}
System.out.println((int) 1.9); // cast truncates toward zero
System.out.println(String.valueOf(123)); // the strval analog
}
}
42 NumberFormatException 1 123
JavaScript v26.13.1
console.log(parseInt("123abc")); // 123 - prefix parsing, the intval twin
console.log(parseInt("3.14xyz")); // 3 - stops at the dot
console.log(Number("123abc")); // NaN - strict whole-string conversion
console.log(Number("3.14")); // 3.14
console.log(Boolean("0")); // true! only "" is falsy among strings
console.log(Boolean("")); // false
console.log(String(123)); // "123"
123 3 NaN 3.14 true false 123
PHP v8.6.0
// the four explicit conversion functions - the trap table
var_dump(intval('123abc')); // 123: the numeric PREFIX is taken
var_dump(intval('abc')); // 0: no digits, no error
var_dump(intval(1.9)); // 1: truncation toward zero
var_dump(floatval('3.14xyz')); // 3.14: prefix parsing again
var_dump(boolval('0')); // false: the ONLY falsy string
var_dump(boolval('0.0')); // true: "0.0" is a different string than "0"
var_dump(boolval('')); // false: empty string
var_dump(strval(123)); // "123"
var_dump(intval('42abc') === (int) '42abc'); // function call == cast operator
int(123) int(0) int(1) float(3.14) bool(false) bool(true) bool(false) string(3) "123" bool(true)
Python v3.15.2
print(int("42")) # strict: the WHOLE string must parse
try:
print(int("123abc")) # no prefix parsing - raises
except ValueError as e:
print("ValueError")
print(float("3.14"))
try:
float("3.14xyz")
except ValueError:
print("ValueError")
print(bool("0")) # True! non-empty string is truthy - PHP's "0" trap is absent
print(bool("")) # False: only empty string is falsy
print(str(123))
42 ValueError 3.14 ValueError True False 123
Rust v1.99.1
fn main() {
println!("{}", "42".parse::<i32>().unwrap());
println!("{}", "123abc".parse::<i32>().is_err()); // strict: Err, no prefix
println!("{}", "3.14".parse::<f64>().unwrap() == 3.14);
println!("{}", "true".parse::<bool>().unwrap()); // bool accepts "true"/"false" only
println!("{}", "0".parse::<bool>().is_err()); // "0" is not a bool in Rust
println!("{}", 123.to_string());
// truncation toward zero is an explicit method, not a silent cast
println!("{}", 1.9_f64 as i32);
}
42 true true true true 123 1
TypeScript v26.13.1
console.log(parseInt("123abc")); // 123 - prefix parsing, the intval twin
console.log(parseInt("3.14xyz")); // 3
console.log(Number("123abc")); // NaN - strict whole-string conversion
console.log(Boolean("0")); // true! only "" is falsy among strings
console.log(String(123)); // "123"
123 3 NaN true 123
Language differences
C: strtol is the ancestor of intval's prefix parsing (PHP's numeric conversion springs from it); the (int) cast truncates toward zero; C has no native string-to-bool conversion, and the idiom is x != 0.
C++: std::stoi/stof inherits C's prefix parsing ("123abc" -> 123) but THROWS on unparseable input (std::invalid_argument) instead of returning 0 - one notch stricter than PHP; the (int) cast also truncates toward zero.
C#: the exception camp plus a TryParse double track - int.Parse is strict and throws, int.TryParse returns bool without throwing; bool.Parse/Convert.ToBoolean accept only "True"/"False" ("0" throws FormatException outright - stricter than PHP treating "0" as falsy).
Go: fully strict - strconv.Atoi/ParseFloat/ParseBool all return (value, err) and never parse prefixes; ParseBool also accepts "0" as false (six spellings: t/f/true/false/1/0).
Java: fully exception-based - Integer.parseInt is strict and throws NumberFormatException on failure; the (int) cast truncates toward zero; String.valueOf is the strval counterpart.
JavaScript: a two-function teaching contrast - parseInt parses prefixes ("123abc" -> 123, the same trap as intval) while Number() is strict across the whole string (-> NaN); among strings only "" is falsy and "0" is true (PHP's boolval("0")=false is the odd one out).
PHP: the four functions are the function versions of the explicit cast operators - intval('123abc') takes the prefix and returns 123 (descended from C's strtol) without erroring; boolval's falsy table: false/0/0.0/""/"0"/[]/null ("0" is the ONLY falsy string - a historical burden from HTML form/database zero-string consistency); floatval is aliased as doubleval.
Python: fully exception-based - int()/float() parse the whole string and raise ValueError on failure, with no prefix parsing; bool("0") is True (non-empty means true), so PHP's "0" trap does not exist.
Rust: parse::<T>() returns a Result and is fully strict - no prefix parsing; bool accepts only "true"/"false" ("0" is simply Err); the as cast from float to int truncates toward zero but saturates on overflow (1e300 as i32 = i32::MAX), confining the silence to a single method.
TypeScript: same as JavaScript.