-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.
– eliangela
It makes no sense for you to declare the string as
$mes = (string) 08
, it would be easier to declare direct as string– Wallace Maxters
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.
– eliangela
@eliangela ah, it makes sense. Maybe it’s his doubt. If you want to fill in with
0
in front, use the functionsprintf
with the amount of0
desired. Example:$mes = sprintf('%02d', 8)
– Wallace Maxters
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.
– Jonathan Paulista
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?
– Wallace Maxters
@Wallacemaxter, Values 08 and 09 cannot be converted to string. The language does not accept this conversion. This is the definitive answer?
– Jonathan Paulista
Yes. And honestly, I don’t understand why you want to define a value like
int
to get astring
, and you can already define astring
directly. Furthermore, depending on what you want to do, you could be gaining time by doing it in an easier (or less complicated).– Wallace Maxters
@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
– Jonathan Paulista
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.
– Woss
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 oncase
and converts what you receive toint
? It would be too easy to solve this.– Wallace Maxters
$mes = (int) $_GET['mes']
... Then you use aarray
months-long.– Wallace Maxters
The title implies that the problem is with octal, but it seems to me that actually the problem there is another.
– Wallace Maxters
@Wallacemaxters, $mes = (int) 09; echo $mes; // returns 0;
– Jonathan Paulista
The answer you’re looking for is don’t use
08
(value does not exist), use"08"
.– Woss
@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.
– Wallace Maxters
@Wallacemaxters Values 08 and 09 cannot be converted to string. The language does not accept this conversion. This is the definitive answer?
– Jonathan Paulista