Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

Left shift with assignment (<<=)

Store the left-shifted bits to make room for another field.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

$value <<= $operand

Store the left-shifted bits to make room for another field. Once the high bit has been shifted out, a later right shift cannot recover it.

Move a bit field toward more significant positions. Zeros fill the low positions and bits shifted beyond the integer width are lost. The right operand gives the number of bit positions. A negative shift count raises ArithmeticError. Shifting by zero positions preserves the number.

Evaluation

  • $value = 3; $value <<= 2; → 12
  • $value = 7; $value <<= 0; → 7
  • $value = (-3); $value <<= 1; → -6

Examples

<?php
$value = 3;
$value <<= 2;
echo json_encode($value), PHP_EOL;

$value = 7;
$value <<= 0;
echo json_encode($value), PHP_EOL;

$value = (-3);
$value <<= 1;
echo json_encode($value), PHP_EOL;

Result

12
7
-6

Sources