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
| Function | Purpose |
|---|---|
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
| Mistake | Correct Approach |
|---|---|
| Ignoring this behavior: Negative numbers are represented using platform-dependent two’s-complement bit width | Negative 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.