0

A very simple code in php

<?php

$aaa    = $_GET["aaa"];
echo $aaa + "\n";

$bbb    = $_GET["bbb"];
echo $bbb+ "\n";

$ccc    = $_GET["ccc"];
echo $ccc+ "\n";

the URL calling this

http://example.com?aaa=AAA&bbb=BBB&ccc=CCC

the result is completely blank. Nothing gets printed.

If I change the echos to

echo (string) ($aaa + "\n");
echo (string) ($bbb + "\n");
echo (string) ($ccc + "\n");

then the result is

000

Why?

Duck
  • 34,902
  • 47
  • 248
  • 470
  • 1
    Because you are adding, not concatenating those values. `.` is the concatenation operator in PHP. PHP is using type juggling because of this and your strings become their integer equivalents which is zero and thus zero plus zero equals zero which is what you see in your output. – John Conde Apr 26 '20 at 21:56
  • sorry about that. I was programming in javascript at the same time... – Duck Apr 27 '20 at 13:07

1 Answers1

1

Different from JS or Python for example, where the string concatenate sign is a "+", in PHP it is a ".", "+" in PHP is used for numerical sums. Try this:

<?php

$aaa = $_GET["aaa"];
echo $aaa . "\n";

$bbb = $_GET["bbb"];
echo $bbb . "\n";

$ccc = $_GET["ccc"];
echo $ccc . "\n";
Fellipe Sanches
  • 7,395
  • 4
  • 32
  • 33