Skip to main content

Types, Comparisons, and Values

Core PHP Types

TypeExampleNotes
int42Whole number
float3.14Decimal number
string'hello'Text or binary-safe byte string
booltrueBoolean value
array['a', 'b']Ordered map
object$postClass instance
nullnullNo value
resourceFile handleLegacy 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

ExpressionMeaning
$a == $bEqual after type coercion
$a === $bEqual and same type
$a != $bNot equal after type coercion
$a !== $bNot 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 PatternExampleHow To Check
false on missingget_option()false === $value
WP_Error on failurewp_remote_get()is_wp_error($value)
0 on missing IDget_current_user_id()0 === $user_id
Empty arrayget_posts()[] === $posts
Object or nullget_post()$post instanceof WP_Post

Common WordPress Type Normalizers

Input GoalFunction
Positive integerabsint()
Key or slugsanitize_key()
Textsanitize_text_field()
Textareasanitize_textarea_field()
Emailsanitize_email()
URL for storageesc_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

FunctionBehavior
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_Error before reading HTTP or media responses.
  • Avoid truthiness when 0 or empty string are valid values.
  • Do not add modern type syntax unless production PHP supports it.