Working with PHP Octals and String conversions

99封情书 提交于 2019-12-01 05:27:00

When you convert with (string) $number, you always get a string in decimal base, it doesn't matter if you write the number in octal mode or in decimal mode, an int is an int and it has not itself a base. It's his string representation that have to be interpreted with a base.

You can get the octal string representation of a number in this way:

$str = base_convert((string) 00032432, 10, 8);

or giving the number in decimal rep:

$str = base_convert((string) 13594, 10, 8);    

or, more concisely but less clearly:

 $str = base_convert(00032432, 10, 8);
 $str = base_convert(13594, 10, 8);

In the last the string conversion is made implicitly. The examples give all as result $str = "32432".

base_convert converts a string representation of a number from a base to another

If you want also the zeros in your string, you can add them with simple math.

Hope this can help you.

To convert an octal to a string, cast it:

$str = (string) 00032432;

You can convert between octal and decimal with the functions octdec and decoct

<?php
echo octdec('77') . "\n";
echo octdec(decoct(45));
?>

http://uk.php.net/manual/en/function.octdec.php

$str = sprintf('%o', $octal_value);
$serial = 00032432; //here you have an octal number
$serial= strval($serial); //now you have a string
if ($serial[0]=="0") {
 //do something
}

Everything from the database is automatically a string. Integers are strings, dates are strings, dogs are strings.

If you are doing something weird, convert anything to a string by: $a = (string) 12;

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!