Differencebetween .= and += in PHP

What is the difference between ' and in PHP?

Basically, single-quoted strings are plain text with virtually no special case whereas double-quoted strings have variable interpolation (e.g. echo "Hello $username";) as well as escaped sequences such as "\n" (newline.)

You can learn more about strings in PHP's manual.

Difference between | and || in PHP

var_dump(($value > 0) || (strlen($string) == 2));

|| is a logical logical operatpor, see http://php.net/manual/de/language.operators.logical.php

var_dump(($value > 0) | (strlen($string) == 2));

| is a bitwise operator, see http://php.net/manual/de/language.operators.bitwise.php

Sure, you can change | to ||, but you won't get the same result ;) A little explanation for your code, but you should really read the doc for bit- and logical operators:

You already answered, that both don't do the same:

var_dump(($value < 0) || (strlen($string) == 2)); -> returns a boolean true

var_dump(($value < 0) | (strlen($string) == 2)); -> returns an integer 1

If you do:

var_dump(true === 1);

You will get false, because integer 1 isn't a boolean true, even if:

var_dump(true == 1);

or

var_dump(true === (bool)1);

will return true (== doesn't check for type, see the docs, and (bool) casts the integer 1 to be a boolean true (see http://php.net/manual/de/language.types.boolean.php#language.types.boolean.casting to know what is false and what is true).

What is the difference 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".

Difference between # and // in php?

For me it's easier to type // by double pressing the key on my keyboard moving just my right pinky one key down and pressing it two times.

If I want to do # I need to use both hands and the movements are "bigger" ;). It's the same for echo and print.

But in print and echo "scenario" you can hear an argument that one function is a little slower, however I am not sure right now which one ;) but it's really something that is no deal-breaker when optimizing for code I guess.

According to this topic echo is a little faster:
Should I use echo or print in php scripts?

Whats is the difference between - and [''] in php

That's a misconception.

The upper syntax with square brackets will only work for arrays (or objects implementing ArrayAccess).

The lower syntax using the T_OBJECT_OPERATOR will only work on objects. You cannot access arrays like that.

Compare the following links in the PHP manual:

  • http://php.net/arrays
  • http://php.net/manual/en/language.oop5.php

Is there a difference between !== and != in PHP?

The != operator compares value, while the !== operator compares type as well.

That means this:

var_dump(5!="5"); // bool(false)
var_dump(5!=="5"); // bool(true), because "5" and 5 are of different types

What is the difference between || and or in PHP?

"||" has a greater precedence than "or".

An example (from the PHP documentation):

<?php
// "||" has a greater precedence than "or"
$e = false || true; // $e will be assigned to (false || true) which is true
$f = false or true; // $f will be assigned to false
var_dump($e, $f);
?>

Read more here: Logical Operators

Difference between ./ and ../ in PHP

This is a system thing and NOT just a PHP thing.

The ./ indicates the current directory. If you ever list the contents of a *nix system you will get the following at the top.

.
../

The top one (.) is the same as ./ which means "this directory". So if including a file like such:

include('./config.php')

You are telling PHP to look in the current directory for "config.php". Which is the same as

include('config.php')

The ../ indicates the directory above or "parent directory"

include('../config.php')

This is telling PHP to go one directory up and look for "config.php". These commands can be chained like so:

../../config.php

This tells the system to go up one directory, go up again and then look for "config.php"

Is there a difference between '?=' and '?php'?

<?= is not the same as <?php

<?= is the same as <?php echo

<? is the same as <?php



Related Topics



Leave a reply



Submit