In PHP, What Is the Differences Between Null and Setting a String to Equal 2 Single Quotes

In PHP, what is the differences between NULL and setting a string to equal 2 single quotes

Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.

"", 0, "0", False, array(), Null are all considered False in PHP.

Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().

$x = false;
isset($x) -> true
echo $x -> ""

$y = null;
isset($y) -> false
echo $y -> ""

//$z is not set
isset($z) -> false
echo $z -> E_NOTICE

So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.

When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by NULL with no quotes.

So if you want an EMPTY field, set it to ""

INSERT INTO foo SET bar = ""

But if you want a NULL field, set it to NULL

INSERT INTO foo SET bar = NULL

BIG DIFFERENCE.

But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).

diffrenece between $a=''; and $a=NULL; in php

do you mean $a = '' or $ a = ""

If so $a = "" or '' means that variable $a is being set equal to an empty string. In contrast $a = NULL means that variable $a is being set to a special PHP constant NULL which is effectively nothing. The major difference is that $a = '' sets $a as a string variable whereas $a = NULL doesn't. This tends to matter more in languages that require strict declaration of variable types.

See here for more info on NULL: http://php.net/manual/en/language.types.null.php

Any difference between init a member varible with null or not?

There is no difference between these two variable both of consider as null

-------------------------------------------------------------------------------
| Expression | gettype() | empty() | is_null() | isset() | boolean |
-------------------------------------------------------------------------------
| $f = null; | NULL | TRUE | TRUE | FALSE | FALSE |
| $f; | NULL | TRUE | TRUE | FALSE | FALSE |
-------------------------------------------------------------------------------

This is from @RexM answers

null is a special placeholder value in the programming language that literally means "nothing". It's not 0, it's not an empty string, it's nothing. There is no value in memory being pointed to. An empty string, on the other hand, is still a string object, just a very short one :)

What is the difference between single-quoted and double-quoted strings in PHP?

PHP strings can be specified not just in two ways, but in four ways.

  1. Single quoted strings will display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\ (So yes, even single quoted strings are parsed).
  2. Double quote strings will display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable $type and you want to echo "The $types are". That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign. Take a look at string parsing to see how to use array variables and such.
  3. Heredoc string syntax works like double quoted strings. It starts with <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.
  4. Nowdoc (since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. No parsing is done in nowdoc.

Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3 (Useless Optimizations toward the bottom, section C). Also, this benchmarks page has a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.

When should I use double or single quotes in JavaScript?

The most likely reason for use of single vs. double in different libraries is programmer preference and/or API consistency. Other than being consistent, use whichever best suits the string.

Using the other type of quote as a literal:

alert('Say "Hello"');
alert("Say 'Hello'");

This can get complicated:

alert("It's \"game\" time.");
alert('It\'s "game" time.');

Another option, new in ECMAScript 6, is template literals which use the backtick character:

alert(`Use "double" and 'single' quotes in the same string`);
alert(`Escape the \` back-tick character and the \${ dollar-brace sequence in a string`);

Template literals offer a clean syntax for: variable interpolation, multi-line strings, and more.

Note that JSON is formally specified to use double quotes, which may be worth considering depending on system requirements.



Related Topics



Leave a reply



Submit