Asked  7 Months ago    Answers:  2   Viewed   34 times

What are the differences between .= and += in PHP?



Quite simply, "+=" is a numeric operator and ".=" is a string operator. Consider this example:

$a = 'this is a ';
$a += 'test';

This is like writing:

$a = 'this' + 'test';

The "+" or "+=" operator first converts the values to integers (and all strings evaluate to zero when cast to ints) and then adds them, so you get 0.

If you do this:

$a = 10;
$a .= 5;

This is the same as writing:

$a = 10 . 5;

Since the "." operator is a string operator, it first converts the values to strings; and since "." means "concatenate," the result is the string "105".

Wednesday, March 31, 2021
answered 7 Months ago

When running under Linux or MacOS, PHP only allows / as a directory separator.

When running under Windows, PHP accepts either / or as a directory separator; it treats them exactly the same.

In virtually all cases, it is better to always use /, because that will allow your code to run on any platform. If you use for directory separators, then your code will only work on Windows.

The difference on Windows is to allow compatibility with other software that might provide paths with separators, but unless you specifically need to do that, it's best to stick with /.

Also, there is another use of in PHP which might be confusing you (particularly as you mentioned PSR0). The is also the separator for PHP namespaces.

Namespaces are not the same as directory separators, but they can end up looking like them, because common practice is to organise a project such that the namespaces match the directory structure. This is done to make your code modules easy to find and easy to write an autoloader for them, and it is thus the recommended way of structuring a project as per PSR0, but it is not compulsory in the PHP language; namespaces are not the same as directory paths.

Wednesday, March 31, 2021
answered 7 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :