Types, Comparisons, and Values
Core PHP Types
| Type | Example | Notes |
|---|---|---|
int | 42 | Whole number |
float | 3.14 | Decimal number |
string | 'hello' | Text or binary-safe byte string |
bool | true | Boolean value |
array | ['a', 'b'] | Ordered map |
object | $post | Class instance |
null | null | No value |
resource | File handle | Legacy external resource handle |
Type Checks
is_int($value);
is_float($value);
is_string($value);
is_bool($value);
is_array($value);
is_object($value);
is_null($value);
is_numeric($value);
Casting
$id = (int) $raw_id;
$price = (float) $raw_price;
$label = (string) $raw_label;
$enabled = (bool) $raw_enabled;
$items = (array) $raw_items;
In WordPress, prefer semantic normalizers when available.
$post_id = absint($_GET['post_id'] ?? 0);
$key = sanitize_key($_GET['key'] ?? '');
Truthy and Falsy
Falsy values include false, 0, 0.0, '', '0', [], and null. Everything else is truthy.
if ($value) {
// Runs for truthy values.
}
Be careful with saved options that may legitimately be '0'.
Loose vs Strict Comparison
| Expression | Meaning |
|---|---|
$a == $b | Equal after type coercion |
$a === $b | Equal and same type |
$a != $b | Not equal after type coercion |
$a !== $b | Not equal or different type |
Prefer strict comparison.
if (false === get_option('myplugin_missing_value')) {
// Option does not exist.
}
Null Coalescing and Ternary
$value = $array['key'] ?? 'default';
$label = $enabled ? 'Enabled' : 'Disabled';
Avoid ternary shorthand when 0, '0', or '' are valid values.
Typed Parameters and Returns
function myplugin_format_count(int $count): string {
return number_format_i18n($count);
}
PHP 8+ supports union types.
function myplugin_normalize_id(int|string $raw_id): int {
return absint($raw_id);
}
Use only when the target PHP version supports it.
WordPress Return Value Patterns
| API Pattern | Example | How To Check |
|---|---|---|
false on missing | get_option() | false === $value |
WP_Error on failure | wp_remote_get() | is_wp_error($value) |
0 on missing ID | get_current_user_id() | 0 === $user_id |
| Empty array | get_posts() | [] === $posts |
| Object or null | get_post() | $post instanceof WP_Post |
Common WordPress Type Normalizers
| Input Goal | Function |
|---|---|
| Positive integer | absint() |
| Key or slug | sanitize_key() |
| Text | sanitize_text_field() |
| Textarea | sanitize_textarea_field() |
sanitize_email() | |
| URL for storage | esc_url_raw() |
| Boolean checkbox | ! empty($value) |
Checking Arrays and Objects
$settings = get_option('myplugin_settings', []);
if (! is_array($settings)) {
$settings = [];
}
$limit = isset($settings['limit']) ? absint($settings['limit']) : 10;
$post = get_post($post_id);
if (! $post instanceof WP_Post) {
return;
}
empty, isset, array_key_exists
| Function | Behavior |
|---|---|
isset($x) | False if missing or null |
empty($x) | True if missing or falsy |
array_key_exists('k', $a) | True if key exists, even null |
Production Checklist
- Use strict comparisons for sentinel values like
false. - Normalize IDs with
absint(). - Confirm arrays are arrays before reading nested keys.
- Check
WP_Errorbefore reading HTTP or media responses. - Avoid truthiness when
0or empty string are valid values. - Do not add modern type syntax unless production PHP supports it.