Content · Programming · PHP

PHP dechex() Function: Usage and Examples

Learn how PHP dechex works with practical examples, important behavior, relevant alternatives and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

Converts an integer into a hexadecimal string.

What Is dechex() in PHP?

Converts an integer into a hexadecimal string.

Its syntax is:

dechex(int $num): string

Parameters:

  • $num — Accepted as int; required argument.

Return type: string. It does not add a 0x prefix and negative values use the underlying integer representation.

Availability: Core PHP.

Example 1: Basic Usage

<?php

echo dechex(255);

Output:

ff

It does not add a 0x prefix and negative values use the underlying integer representation.

Example 2: A Different Input

<?php

echo dechex(4095);

Output:

fff

It does not add a 0x prefix and negative values use the underlying integer representation.

Practical Example: Applying It in a Program

<?php

$channels = [16, 128, 255]; foreach ($channels as $channel) { echo str_pad(dechex($channel), 2, "0", STR_PAD_LEFT) . PHP_EOL; }

Output:

10
80
ff

It does not add a 0x prefix and negative values use the underlying integer representation.

Important Behavior and Edge Cases

  • Key behavior: It does not add a 0x prefix and negative values use the underlying integer representation.
  • Environment: Core PHP.

Comparison With Related Functions

FunctionPurpose
dechex()Converts an integer into a hexadecimal string.
hexdec()hexdec() parses hexadecimal text as a number; dechex() turns an integer into hexadecimal text.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: It does not add a 0x prefix and negative values use the underlying integer representationIt does not add a 0x prefix and negative values use the underlying integer representation.

Key Takeaways

  • Converts an integer into a hexadecimal string.
  • It does not add a 0x prefix and negative values use the underlying integer representation.
  • Core PHP.

Summary: It does not add a 0x prefix and negative values use the underlying integer representation.

Share this resourceSend this page to someone who may find it useful.