Asked  2 Years ago    Answers:  5   Viewed   77 times

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

 Answers

1

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".

Thursday, September 1, 2022
1

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.

Sunday, November 20, 2022
1

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Sunday, July 31, 2022
 
4

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Sunday, November 6, 2022
5

Why do I have to use "{{title}}" with '@' and "title" with '='?

@ binds a local/directive scope property to the evaluated value of the DOM attribute. If you use title=title1 or title="title1", the value of DOM attribute "title" is simply the string title1. If you use title="{{title}}", the value of the DOM attribute "title" is the interpolated value of {{title}}, hence the string will be whatever parent scope property "title" is currently set to. Since attribute values are always strings, you will always end up with a string value for this property in the directive's scope when using @.

= binds a local/directive scope property to a parent scope property. So with =, you use the parent model/scope property name as the value of the DOM attribute. You can't use {{}}s with =.

With @, you can do things like title="{{title}} and then some" -- {{title}} is interpolated, then the string "and them some" is concatenated with it. The final concatenated string is what the local/directive scope property gets. (You can't do this with =, only @.)

With @, you will need to use attr.$observe('title', function(value) { ... }) if you need to use the value in your link(ing) function. E.g., if(scope.title == "...") won't work like you expect. Note that this means you can only access this attribute asynchronously. You don't need to use $observe() if you are only using the value in a template. E.g., template: '<div>{{title}}</div>'.

With =, you don't need to use $observe.

Can I also access the parent scope directly, without decorating my element with an attribute?

Yes, but only if you don't use an isolate scope. Remove this line from your directive

scope: { ... }

and then your directive will not create a new scope. It will use the parent scope. You can then access all of the parent scope properties directly.

The documentation says "Often it's desirable to pass data from the isolated scope via an expression and to the parent scope", but that seems to work fine with bidirectional binding too. Why would the expression route be better?

Yes, bidirectional binding allows the local/directive scope and the parent scope to share data. "Expression binding" allows the directive to call an expression (or function) defined by a DOM attribute -- and you can also pass data as arguments to the expression or function. So, if you don't need to share data with the parent -- you just want to call a function defined in the parent scope -- you can use the & syntax.

See also

  • Lukas's isolated scope blog post (covers @, =, &)
  • dnc253's explanation of @ and =
  • my blog-like answer about scopes -- the directives section (way at the bottom, just before the Summary section) has a picture of an isolate scope and its parent scope -- the directive scope uses @ for one property and = for another
  • What is the difference between & vs @ and = in angularJS
Sunday, December 18, 2022
5

Single quoted items are always atoms.

The meaning of double quotes depends on the Prolog flag double_quotes:


atom — with this value "a" = a. Nowadays, this is rarely used. But you will find Prolog books where ["abc.pl"] is written.


codes — a list of character codes. This is frequently the default, but it leads to very unreadable answers like

?- set_prolog_flag(double_quotes,codes).
true.

?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
Satz = [74,97,32,116,118,111,105,32,115,108,117,103,97,33] ;
Satz = [74,97,32,116,118,111,105,32,114,97,98,111,116,110,105,107,33].

Even worse, if you use characters beyond ASCII:

?- phrase(("? ???? ",("?????"|"????????"),"!"), Satz).
Satz = [1071,32,1090,1074,1086,1081,32,1089,1083,1091,1075,1072,33] ;
Satz = [1071,32,1090,1074,1086,1081,32,1088,1072,1073,1086,1090,1085,1080,1082,33].

chars — a list of one-char atoms. See this for more about it.

?- set_prolog_flag(double_quotes,chars).
true.

?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
Satz = ['J',a,' ',t,v,o,i,' ',s,l,u,g,a,!] ;
Satz = ['J',a,' ',t,v,o,i,' ',r,a,b,o,t,n,i,k,!].

?- phrase(("? ???? ",("?????"|"????????"),"!"), Satz).
Satz = ['?',' ',?,?,?,?,' ',?,?,?,?,?,!] ;
Satz = ['?',' ',?,?,?,?,' ',?,?,?,?,?,?,?,?,!].

This notation gives more readable answers and is the default in Scryer which displays them even more compactly with the double quote notation for printing any list of one-char atoms. For SICStus and SWI this can be emulated with the following library.

?- use_module(library(double_quotes)).
true.

?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
Satz = "Ja tvoi sluga!" ;
Satz = "Ja tvoi rabotnik!".

?- phrase(("? ???? ",("?????"|"????????"),"!"), Satz).
Satz = "? ???? ?????!" ;
Satz = "? ???? ????????!".

If you have difficulties installing double_quotes.pl as a library, simply put it into the directory of your other Prolog files and say: use_module(double_quotes).

Friday, November 18, 2022
 
1

The = "operator" in Prolog is actually a predicate (with infix notation) =/2 that succeeds when the two terms are unified. Thus X = 2 or 2 = X amount to the same thing, a goal to unify X with 2.

The == "operator" differs in that it succeeds only if the two terms are already identical without further unification. Thus X == 2 is true only if the variable X had previously been assigned the value 2.

Added: It's interesting to work through what happens when "not" gets mixed into these goals, per the comment by JohnS below. See the nice set of examples in the Amzi! Prolog documentation.

= means the two terms cannot be unified, i.e. that unification fails. As with all applications of negation as failure, "not unified" does not (and cannot) result in any unification between terms.

== means the two terms are not identical. Here also no unification takes place even if this succeeds.

Finally think about what not(not(X = Y)) will do. The inner goal succeeds if X and Y (which can be arbitrary terms) can be unified, and so will the double negation of that. However wrapping the inner goal inside the double negation produces a goal that succeeds if the two terms can be unified but without unifying those terms.

It is left as an exercise for the reader to contemplate whether not(not(X == Y)) has any similar utility.

Wednesday, August 3, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages