Difference Between <PHP and <

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 ?php and ?

The first is a safe open and close tag variation, the second is the so called short-open tag. The second one is not always available, use the first option if it's possible.
You could check the availability of short open tags in php.ini, at the short_open_tag.

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?

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

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

<?= is not the same as <?php

<?= is the same as <?php echo

<? is the same as <?php

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 PHP ??, ?:, and ??=

To combine all answers already made in former questions. With ?? you check for existence and not being null, with ?: you check for true/false.

$result = $a ?? $b

is the same as:

if (isset($a) && !is_null($a)) {
$result = $a;
} else {
$result = $b;
}

The ??= is a shortcut for ?? and it works like .= or += as well, which means you can shorten

$a = $a ?? $b;

to

$a ??= $b;

The ?: is a shortcut for the ternary operator which is also a shortcut:

if ($a) {
$result = $a;
} else {
$result = $b;
}

is the same as:

$result = $a ? $a : $b;

is the same as:

$result = $a ?: $b;

You could both describe as: The first operand is the value to check, and if this one is useful, take it, if not, take the second as default.

You can concatenate them like this one:

$user = $_POST['user'] ?? $_SESSION['user'] ?? $_COOKIE['user'] ?? '';

Which means something like: If you get the username in the POST array because of just logging in, fine, take it, if not, see whether the user is already logged on and in the session, if yes, take it, if not, maybe we have a user in the Cookies (checked the "remember me" checkbox while last login), and if this fails also, well, then leave it empty.

Of course you have to check the password too. ;-)

Edit: I use the ?: operator very often to ensure that I have a certain datatype in a variable. There are a lot PHP functions that give back maybe a string or an array if successful, but false if some error occured. Instead of having false as result, having the empty string or empty array is more consistent. As example, scandir should give back an array, but gives back false if an error occures. So you can do this:

$files = scandir('/some/random/folder') ?: [];

Then $files will always be an array, even if scandir fails. Well, then it's an empty array, but maybe better than checking later against false. In fact, we already did it with very low effort. Then you can use a foreach loop without some wrapping if. If scandir failed, the array is empty and the loop will just do nothing.

Difference between ?php and ??

<? is called short tag that it is discouraged because it needs a specific php.ini setting to be enabled. I've ran into a few shared servers where <? is disabled. Therefore you should try to use <?php instead.

You can also check the documentation.

What difference between .php and abc.blade.php when i give file names in laravel 5?

Files ending in .blade.php will allow you to use the Blade templating language while "normal" .php files won't. Note that you can use PHP code in both files with the usual <?php ... ?>



Related Topics



Leave a reply



Submit