How to convert a value (which appears to be octal) to literal string in PHP (5.6)?

Asked

Viewed 97 times

-4

How to convert a value (which appears to be an octal) to a literal string in PHP (5.6)? Example:

//considere $mes=08; ou $mes=09;
$mes = 08;

echo $mes; // Retorna 0

$result = (string) $mes;
echo $result; // Retorna 0

$result = sprintf('%o', $mes);
echo $result; // Retorna 0

$result = base_convert($mes, 8, 10);
echo $result; // Retorna 0

The value you "would like" to find for $result is '08' (String).

Example of use 1:

$mes = (string) 08;

....
public function mesPorExtenso($mes) {

    switch ($mes) {
        case '08': //Não entra no case porque $mes é 0
            $mes = "Agosto";
        break;
    }
}
...

Example of use 2:

$mes = 08;

....
public function mesPorExtenso($mes) {
    switch ($mes) {
        case 08: // PHP Parse error: Invalid numeric literal
            $mes = "Agosto";
        break;
    }
}
...

What is the correct way to convert 08 to 08 string'?

The mesPorExtenso() method is called dozens of times, The value is defined by who calls it. I just want to make sure that the variable $mes be a string.

  • First, I believe you don’t want to convert an octal to String, but a number to String. Octal is a base 8 system, meaning it has representation with numbers from 0 to 7.

  • 1

    It makes no sense for you to declare the string as $mes = (string) 08, it would be easier to declare direct as string

  • 1

    As @Wallacemaxters said, it doesn’t make sense even if you declare it that way. And from what I understand, PHP is not accepting the number with zero in front. If you want to complete with zeros on the left, you can use the function str_pad of PHP.

  • 1

    @eliangela ah, it makes sense. Maybe it’s his doubt. If you want to fill in with 0 in front, use the function sprintf with the amount of 0 desired. Example: $mes = sprintf('%02d', 8)

  • The variable $mes, in this case, comes from external inputs, that is, I do not directly declare the variable. It already comes with the "0" in front, with the values: 07, 08, 09, 10, 11 or 12. That is precisely the question.

  • Ronaldo, the example does not apply, but what do you want ? Why do you want to convert a value, which the language does not accept?

  • @Wallacemaxter, Values 08 and 09 cannot be converted to string. The language does not accept this conversion. This is the definitive answer?

  • 1

    Yes. And honestly, I don’t understand why you want to define a value like int to get a string, and you can already define a string directly. Furthermore, depending on what you want to do, you could be gaining time by doing it in an easier (or less complicated).

  • @Wallacemaxters imagine the following situation: public Function mesPorExtenso($mes) { switch ($mes) { case '08': //Does not enter the case because $mes is 0 Return "August"; break; } } Imagine that this method is used by hundreds of classes. as I guarantee that month is a string

  • 4

    To say that 08 or 09 is an octal number is to say that 02 is a binary representation, that the day 32 of the month 15 exists, that it is now 25 hours and 75 minutes, etc. They are mathematically impossible values for their respective bases. In your case displays 0 by the PHP philosophy of never telling the developer that he is wrong.

  • 1

    is_string($mes)? strlen($mes) == 2? Dude, there are a thousand ways to do this [easy] in php. With all due respect, you’re complicating it. Besides, why don’t you use a numerical representation on case and converts what you receive to int? It would be too easy to solve this.

  • $mes = (int) $_GET['mes']... Then you use a array months-long.

  • 2

    The title implies that the problem is with octal, but it seems to me that actually the problem there is another.

  • @Wallacemaxters, $mes = (int) 09; echo $mes; // returns 0;

  • 1

    The answer you’re looking for is don’t use 08 (value does not exist), use "08".

  • 3

    @ronaldorodrigues man, man! what is the difficulty of changing the code for it to receive int? You are spending a lot of time on something that almost no one would do. Your solution seems to be more complicated than helping.

  • @Wallacemaxters Values 08 and 09 cannot be converted to string. The language does not accept this conversion. This is the definitive answer?

Show 12 more comments

2 answers

5


The problem is using the 0 when declaring the month. From your web server you will already receive the value in string, then only in the cases that you put directly into the code will have problems.

To function correctly, declare already as string $mes = '08' or use the integer without zero $mes = 8.

Reference in PHP documentation: Whole

  • 1

    I’m trying to explain this to two hours, kkkkkkk

4

When you enter 0 in front of a number, it is considered an octal. The problem is that in the case of values 08 and 09, in versions prior to PHP 7 ignore the rest of the number. From PHP 7 a conversion error will be issued.

Warning In versions prior to PHP 7, if an invalid digit is passed for octal integer (for example, 8 or 9), the rest of the number will be ignored. Since PHP 7, an interpretation error is issued. Information in https://secure.php.net/manual/en/language.types.integer.php

To work what you want, you need to pass a String.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.