Echo returning wrong value

6

I do not understand one thing in this case:

$arr_4[] = "$arr_3[$i]<sup>$arr_22[$i]-$contador+$eletrons</sup>";

Come on, the $ arr_3 [$ i] stores a string value "3d", the $arr_22[$i] = int (10) , the $contador = int(30) %% $eletrons é int 26 . If I echo the array created in this case, it is displayed: -1 only, nor 3d appears.

Now, if I give:

$arr_4[] = "$arr_3[$i]<sup>".$arr_22[$i]-$contador+$eletrons."</sup>"; 

What I think is correct, is not displayed the result of the operation ... In other words, returns me:

   3d^10-30+26.

I want to appear with the operation performed, ie to return:. 3d ^ 6

    
asked by anonymous 13.10.2014 / 18:15

3 answers

7

It's already clear from the other answers and comments that you need to isolate mathematical operations with parentheses. As Sergio suggested:

$arr_4[] = $arr_3[$i]."<sup>".($arr_22[$i]-$contador+$eletrons)."</sup>"

To understand the reason, I checked the operator precedence + , - and . , and found that all three have the same precedence. Since they are associative operators, they are grouped from left to right. Substituting the values of your code, we have, without applying parentheses:

$a = "3d";
$b = 10;
$c = 30;
$d = 26;
$x = "$a<sup>" . $b - $c + $d . "</sup>";  // "-1</sup>"

The output is "-1</sup>" because the code is interpreted like this:

$x = (("3d<sup>" . $b) - $c + $d) . "</sup>";

// portanto:
$x = ("3d<sup>10" - 30 + 26) . "</sup>";  

// 3d<sup>10 é convertido para 3 no momento da subtração:
$x = (3 - 30 + 26) . "</sup>";

// finalmente:
$x = -1 . "</sup>";

The PHP manual explains the conversion of "3d<sup>10" to 3 into a section titled String conversion to numbers .

    
13.10.2014 / 19:38
7

I would do so:

$arr_4[] = $arr_3[$i]."<sup>".($arr_22[$i]-$contador+$eletrons)."</sup>"

The idea of having parentheses is because php does not do correct concatenation of .10 + 30. . Must have .(10 + 30).

    
13.10.2014 / 18:30
6

strings in PHP can be encapsulated with single or double plication.

When encapsulated with double-sided, what happens is that the variables present in the contents of the string are replaced by their value:

<?php
$a = 1;
$b = 2;
$c = 3;

echo "Resultado: $a+$b-$c."; // Resultado: 1+2-3.
?>

To get the result of the operation indicates, we have to give the proper instruction to PHP, making use of the parentheses to indicate that the result of the one between them should be concatenated:

<?php
$a = 1;
$b = 2;
$c = 3;

echo "Resultado: ".($a+$b-$c)."."; // Resultado: 0.
?>

Your case

When we try to perform operations in the concatenation of strings , we have to take into account that + and - takes precedence such as . operator, giving results unexpected.

strings can be concatenated only with the . operator.

Arithmetic operators in the course of concatenation, such as + and - will tell PHP that an arithmetic operation should occur:

Note: PHP will convert non-empty values and non-numeric values to 0 (zero) during an arithmetic operation:

<?php
$a = 1;
$b = 2;
$c = 3;

echo "Resultado: ".$a+$b-$c."."; // -1.
?>

Explain:

Decomposing what is happening in the course of the arithmetic operation:

<?php
$a = 1;
$b = 2;
$c = 3;

echo "Fase 01:<br>";
echo "Resultado: ".$a;
// Output: 'Resultado: 1'
// (concatenação normal)

echo "Fase 02:<br>";
echo "Resultado: ".$a+$b;
// Output: '2'
// ("Resultado: ".$a resulta em 0 dando 0+2 = 2)

echo "Fase 03:<br>";
echo "Resultado: ".$a+$b-$c;
// Output: '-1'
// (2-3 = -1)

echo "Fase 04:<br>";
echo "Resultado: ".$a+$b-$c.".";
// Output: '-1.'
// (-1 + "." = -1.)
?>

From the example above, you can see the operation performed by PHP until it reaches the value of -1. in output . Anyway, unexpected results are what we can expect when we use arithmetic operators in conjunction with strings concatenation operators.

    
13.10.2014 / 19:41