YouTip LogoYouTip

Func Math Decbin

# PHP decbin() Function The `decbin()` function is a built-in PHP math function used to convert a decimal (base-10) number into its binary (base-2) representation. This tutorial provides a comprehensive guide on how to use `decbin()`, including its syntax, parameters, return values, and practical code examples. --- ## Definition and Usage The `decbin()` function takes a decimal number and returns a string containing its binary equivalent. * **Complementary Function:** To convert a binary string back into a decimal number, use the [bindec()](func-math-bindec.html) function. --- ## Syntax ```php decbin(int $num): string ``` ### Parameter Values | Parameter | Type | Description | | :--- | :--- | :--- | | `num` | `int` | **Required.** The decimal value to convert. If a float or a numeric string is passed, it will be cast to an integer before conversion. | ### Technical Details | Feature | Description | | :--- | :--- | | **Return Value** | A string containing the binary representation of the given decimal number. | | **Return Type** | `string` | | **PHP Version** | PHP 4, PHP 5, PHP 7, PHP 8 | --- ## Code Examples ### Example 1: Basic Conversion The following example demonstrates how to convert various decimal numbers (passed as strings or integers) into binary strings: ```php "; // Output: 11 echo decbin(1) . "
"; // Output: 1 echo decbin(1587) . "
"; // Output: 11000110011 echo decbin(7); // Output: 111 ?> ``` ### Example 2: Working with Large Numbers and Limits On 64-bit systems, the maximum decimal value you can convert is typically $9,223,372,036,854,775,807$ (which is `PHP_INT_MAX`). ```php ``` --- ## Important Considerations ### 1. Handling Negative Numbers When passing negative numbers to `decbin()`, the function treats the integer as an unsigned value. On a 64-bit system, this results in a 64-bit two's complement representation. ```php ``` ### 2. Floating-Point Numbers If you pass a floating-point number to `decbin()`, the fractional part will be discarded (truncated) because the input is implicitly cast to an integer. ```php ```
← Func Math DechexFunc Math Cosh β†’