How to separate values from a variable and save to separate variables?
Example:
$valores = '12';
How to separate and save to other variables? So:
$primeiroValor = 1;
$segundoValor = 2;
How to separate values from a variable and save to separate variables?
Example:
$valores = '12';
How to separate and save to other variables? So:
$primeiroValor = 1;
$segundoValor = 2;
Another solution would be to separate character by character so that you have each one to do what you want. Example, showing each character of a string in a line;
$palavra = "teste";
$tamPalavra = strlen($palavra);
for ($i = 0; $i < $tamPalavra; $i++) {
echo 'Caracter ' . $i . ' = ' . $palavra[$i] . '<br />';
}
The output of this code will be something like:
Character 1 = t
Character 2 = e
Character 3 = s
Character 4 = t
Character 5 = e
This code allows you to work with a string of unknown size and perform the manipulation you want freely.
Hello, you can use the split method, follow how it works and examples.
This method "breaks" the string using a delimiter set by you and returns an array.
Example:
Split (pattern, string, limit)
$strExemple = "este/e/um/exemplo";
$teste = split ('/', $strExemple);
// Saída
// $teste[0] = "este";
// $teste[1] = "e";
// $teste[2] = "um";
// $teste[3] = "exemplo";
As it is always two characters then just do it like this:
$valores = '12';
$primeiroValor = $valores[0];
$segundoValor = $valores[1];
Both answers are right, but it all depends on the way you want it to get the one you want.
So, if you want to separate character by character then the 'Earendul' answer is the best option. However, if the character-by-character separation is not indicated, the 'Wisner Oliveira' solution is recommended ... That said, I wanted to complement the answer with the following:
Split uses "regular expressions" which in many cases reduces performance and as of version 5.3 of php will be a "decrepated" command so avoid.
There is preg_split which is said to be faster between 25% and 50% when compared to Split
If the case is delimited by a character or a set of characters then in most cases "explode" is the most efficient solution.
I hope I have helped.