Search code examples
phpstringintdecodeencode

PHP: Very simple Encode/Decode string


Is there any PHP function that encodes a string to a int value, which later I can decode it back to a string without any key?


Solution

  • Sure, you can convert strings to numbers and vice versa. Consider:

    $a = "" + 1
    gettype($a) // integer
    $b = "$a"
    gettype($b) // string
    

    You can also do type casting with settype().

    If I misunderstood you and you want to encode arbitrary strings, consider using base64_encode() and bas64_decode(). If you want to convert the base 64 string representation to a base 10 integer, simply use base_convert().