Content · Programming · PHP

PHP decbin() Function: Usage and Examples

Learn how PHP decbin 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 binary string.

What Is decbin() in PHP?

Converts an integer into a binary string.

Its syntax is:

decbin(int $num): string

Parameters:

  • $num — Accepted as int; required argument.

Return type: string. Negative numbers are represented using platform-dependent two’s-complement bit width.

Availability: Core PHP.

Example 1: Basic Usage

<?php

echo decbin(10);

Output:

1010

Negative numbers are represented using platform-dependent two’s-complement bit width.

Example 2: A Different Input

<?php

echo decbin(255);

Output:

11111111

Negative numbers are represented using platform-dependent two’s-complement bit width.

Practical Example: Applying It in a Program

<?php

$permissions = [1, 2, 4, 8]; foreach ($permissions as $permission) { echo str_pad(decbin($permission), 4, "0", STR_PAD_LEFT) . PHP_EOL; }

Output:

0001
0010
0100
1000

Negative numbers are represented using platform-dependent two’s-complement bit width.

Important Behavior and Edge Cases

  • Key behavior: Negative numbers are represented using platform-dependent two’s-complement bit width.
  • Environment: Core PHP.

Comparison With Related Functions

FunctionPurpose
decbin()Converts an integer into a binary string.
bindec()bindec() parses binary digits into a number; decbin() encodes an integer as binary text.

Common Beginner Mistakes

MistakeCorrect Approach
Ignoring this behavior: Negative numbers are represented using platform-dependent two’s-complement bit widthNegative numbers are represented using platform-dependent two’s-complement bit width.

Key Takeaways

  • Converts an integer into a binary string.
  • Negative numbers are represented using platform-dependent two’s-complement bit width.
  • Core PHP.

Summary: Negative numbers are represented using platform-dependent two’s-complement bit width.

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