PHP Is Confused When Adding and Concatenating

PHP is confused when adding and concatenating

Both operators the addition + operator and the concatenation . operator have the same operator precedence, but since they are left associative they get evaluated like the following:

echo (("sum:" . $a) + $b);
echo ("sum:" . ($a + $b));

So your first line does the concatenation first and ends up with:

"sum: 1" + 2

(Now since this is a numeric context your string gets converted to an integer and thus you end up with 0 + 2, which then gives you the result 2.)

Confusion on concatenation before equal operator

Concatenation before an equal sign could be referred to as 'append'

e.g.

$a="hello";
$a.=" world";
echo $a;

The output would be:

hello world

The line:

$a.=" world";

is similar to:

$a=$a." world";

php: how can a similar codes results different output?

The multiplication and division operators have a higher precedence than the concatenation operator, so the mathematical operation is evaluated first for those cases, and then combined into the string.

In these cases, the code is equivalent to:

echo "Answer is: " .($first * $second);
echo "Answer is: " .($first / $second);

which gets you the correct answer.

The addition and subtraction operators have the same precedence, and are left associative, so the lines are executed like this:

echo ("Answer is: " . $first) + $second;
echo ("Answer is: " . $first) - $second;

In both cases, the initial string is type-cast to an integer, before the mathematical operation. Because the string does not start with a number, it is equivalent to writing

echo 0 + $second;
echo 0 - $second;

which results in either a positive or negative version of $second.

As other people have said, wrap the mathematical operations in brackets to ensure the expressions are evaluated in the right order. It will also make your code easier to read.

PHP Punctuation Confusion

dot (.) is php's concatenation operator. In english that means it will glue strings together.

static strings are defined as "quoted" text

in PHP's dynamic typing all scalar (numbers, strings) variables can be used as strings

so...

// 3 static strings 
echo "hello" . " " . "world"; // prints 'hello world'

// 2 variables
$a = "hello";
$b = "world";
echo $a . $b ; // prints 'helloworld'; (no space)

// mixed variables and static string
echo $a . " " . $b; // prints 'hello world'

additionally you should know there is a difference between single quoted(') and double quoted(") strings

single quotes are taken literally.

echo '$a $b'; // prints '$a $b' with literal $
echo "$a $b"; // prints 'hello world' where $a and $b are treated as variables

in php, what is the purpose of using concatenation operator (.) for adding values to an array?

In this particular case there is no point in using the concatenation operator. The left hand side of the expression, $errors[] creates a new, empty, element in the $errors array, so concatenating something to it has the same effect as assigning to it. Indeed if you try the following code with and without the concatenation operator, you'll see the results are the same:

$errors = array();
$errors[] .= 'You must enter a value';
print_r($errors);

$errors = array();
$errors[] = 'You must enter a value';
print_r($errors);

Output:

Array
(
[0] => You must enter a value
)
Array
(
[0] => You must enter a value
)

Why is '...' concatenating two numbers in my code?

No this is not the splat/unpacking operator, even thought it might seem like it is. This is just the result of the PHP parsing process. Already writing your code a bit different might clear some confusion:

echo  20.           .           .7;
# ↑ ↑ ↑
# decimal concatenation decimal
# dot dot dot

Now you have to know that .7 is 0.7 and you can omit the 0 in PHP as described in the syntax for float numbers:

DNUM          ([0-9]*[\.]{LNUM}) | ({LNUM}[\.][0-9]*)

So PHP just concatenates those two numbers together and while doing this PHP's type juggling will silently convert both numbers to strings.

So in the end your code is equivalent to:

echo "20" . "0.7";
//Output: "200.7"

Why a full stop, . and not a plus symbol, + , for string concatenation in PHP?

The most obvious reason would probably be that PHP inherits a lot of its syntax from Perl - and Perl uses a dot (.) for string concatenation.

But, we can delve deeper into it and figure out why this was implemented in Perl - the + operator is most commonly used for mathematical equations - it's only used for concatenation in languages in which the variable type can define the way in which the operator works (simple explanation, example is C#)

var intAddition = 1 + 2;
Console.WriteLine(intAddition); // Prints 3
var stringConcat = "1" + "2";
Console.WriteLine(stringConcat); // Prints "12"

^ As you can see, the + operator is used both for concatenation and addition in C#.


Perhaps the reasoning goes lower level and is due to the boolean algebra of logic gates - + means OR in logic gates, whereas . is used as the AND operator - which makes sense when it comes to string concatenation.

It makes sense to have two separate operators, one for concatenation and one for addition - it's just unfortunate that these two can be mixed up due to other languages.

What does a slash do to a string with a dot in php?

The operator + is numeric / mathematical in PHP.
So '/'+$GLOBALS['filefolder']; is equal to 0+13 in your case (php casts both to integer) which actually is 13.

To concatenate use .

$filefolder = '/' . $GLOBALS['filefolder'];
echo $filefolder


Related Topics



Leave a reply



Submit