Echo returning wrong value

Asked

Viewed 183 times

6

I don’t 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 of 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, not 3d appears.

Now, if I give:

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

Which I think is correct, is not displayed the result of the operation... I mean, returns me:

   3d^10-30+26.

I want you to show up with the operation performed, that is, return: 3d 6.

  • You have to do the account first and put in a variable, then concathens with the strings.

  • Have you tried $arr_3[$i]."<sup>".($arr_22[$i]-$contador+$eletrons)."</sup>" ?

  • Oh yes. Dude, you solved.. What’s the parenthesis logic?

  • 'Cause when I do it without it comes right back?

  • Not putting the parentheses he does not know that it is a function, think that "-" for example is part of the string.

  • I tried several ways, but I did not arrive in 3d 10-30+26 from $arr_4[] = "$arr_3[$i]<sup>".$arr_22[$i]-$contador+$eletrons."</sup>"; was that the correct line that generated this result?

Show 1 more comment

3 answers

7

I would do so:

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

The idea of parenting is because php does not concatenate correctly .10 + 30.. You must have .(10 + 30).

  • That’s right, I just didn’t understand why it doesn’t work. That he doesn’t do it was clear, right. rsrs

  • @Zebradomal this has to do with how PHP is built and what concatenation rules are allowed. That way that calculation will be treated as mathematics and concatenated correctly.

  • 1

    The parentheses indicate to PHP that you should concatenate the result of the operation within them. Without the parentheses and you are using " PHP will replace the variable with its value.

  • 2

    @Zebradomal because it’s PHP, you know how it is... :)

  • 2

    @moustache only pq in php "2 bananas" + 5 results in 7 ? hahaha

  • Wow, this is fucking...

Show 1 more comment

7


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

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

To understand why, I went to check on precedence of operators +, - and ., and found that the three have the same precedence. As they are associative operators, they are grouped from left to right. Replacing 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 exit is "-1</sup>" because the code is interpreted this way:

$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" for 3 in a section entitled String Conversion to Numbers.

  • Very good, thank you.

  • Dude, now this 3d10 operation is just php. rsrs

  • @Zebradomal I included a link to the manual part that explains how it works there.

  • Yes, I saw him...

6

As strings in PHP can be encapsulated with single or double plica.

When encapsulated with double plica, 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, using the parentheses to indicate that the result of what is 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 during the concatenation of strings, we have to take into account that the + and the - shall take precedence as the operator ., giving rise to unexpected results.

As strings can be concatenated only with the operator ..

Arithmetic operators in the course of concatenation, such as + and the - will indicate to 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.
?>

Explaining:

Breaking down 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.)
?>

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

  • +1 for those who did not know that plica = quotation marks :)

  • And just like "Resultado: 1" is interpreted as 0, "1Resultado: 1" would be interpreted as 1 (which is what occurs in the example of the question, "3<sup>10" is interpreted as 3).

  • Good too. Congratulations.

Browser other questions tagged

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