1.2.1103. Should Typecast

When typecasting, it is better to use the casting operator, such as (int) or (bool).

Functions such as intval() or settype() are always slower. This is a micro-optimisation, although such conversion may be use multiple times, leading to a larger performance increase.

Note that intval() may also be used to convert an integer to another base. Intval() called with 2 arguments are skipped by this rule.

<?php

// Fast version
$int = (int) $X;

// Slow version
$int = intval($X);

// Convert to base 8 : can't use (int) for that
$int = intval($X, 8);


?>

1.2.1103.1. Suggestions

  • Use a typecast, instead of a functioncall.

1.2.1103.2. Specs

Short name

Type/ShouldTypecast

Rulesets

All, Analyze, CE, CI-checks, Rector

Exakat since

0.8.4

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

High

Features

cast

Examples

xataface, OpenConf

Available in

Entreprise Edition, Community Edition, Exakat Cloud