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

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

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?

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

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"

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 how php and javascript execute a nested function

Its a scope thing. You could have as easily - in javascript - written "var b = function()". "b" is just a variable of type function defined within the scope of the function a. In PHP, both "a" and "b" are global functions, but it's the job of function "a" to define "b", so it won't get defined until "a" is called. Consider this example...

function a($x) {
if ($x) {
function b() { echo "x not empty"; }
} else {
function b() { echo "x empty"; }
}
}
a(1); // Defines function b
b(); // x not empty
a(0); // PHP Fatal error: Cannot redeclare b() (previously declared...

You can see by the failure to redefine "b", that "b" is a real, globally scoped function. Function "a" could use various criteria to define the function for a particular purpose in different runs. Clearly, in this case, it wouldn't make sense to call function "b" before function "a" has decided how to define it.

I don't, by the way, think the example above is very good coding practice, but it does serve to illustrate the point.

The PHP code most similar to your javascript code would be:

function a() {
$b = function() {
echo "'b' says inner";
};
$b(); // Demonstrating the function can be used inside "a"
}
a(); // 'b' says inner

$b is a variable of type function, which can only be used within function "a".



Related Topics



Leave a reply



Submit