Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Store Float As Index

<?php

$array  = [1 => 0,
           // Implicit conversion from float 1.2 to int loses precision
           1.2 => 1,
           // as string, it fits!
           '1.1' => 2,
          ];

With PHP, floats cannot be used directly as array indexes because array keys must be either integers or strings. If you attempt to use a float as an index, PHP will automatically cast it to an integer, potentially causing unexpected behavior. However, you can explicitly cast the float to a string to preserve its precision as an index. Later, due to PHP’s type juggling, you can still perform arithmetic with it seamlessly.

See Also

PHP Features

Last updated: 11 October 2026