/    Sign up×
Community /Pin to ProfileBookmark

double quotes vs null when initializing a variable

$foo=””;
$foo=NULL;

What are the actual differences between the two? Which is representive of best coding practice? What pitfalls/advantages might arise using one over the other?

I understand that you get the same result using either. But I’m looking for some advanced knowledge about using one over the other.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@bokehDec 14.2006 — [code=php]$foo = null;[/code]That sets the variable but gives it no type whereas [code=php]$foo = "";[/code]creates a string. In theory that makes the second option seem bad practice if you were going to use the variable as an array, but in practice it doesn't seem to matter as an empty string can be written to in array mode:[code=php]<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$variable = "";

$variable[] = 1;
?>[/code]
No error reported. I think people coming from other backgrounds would probably find this behaviour very peculiar.
Copy linkTweet thisAlerts:
@pcthugDec 14.2006 — PHP does not require (or support) explicit type definition in variable declaration; a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer. Note that this does NOT change the types of the operands themselves; the only change is in how the operands are evaluated.

[code=php]<?php
$foo = "0"; // $foo is string (ASCII 48)
$foo += 2; // $foo is now an integer (2)
$foo = $foo + 1.3; // $foo is now a float (3.3)
$foo = 5 + "10 Little Piggies"; // $foo is integer (15)
$foo = 5 + "10 Small Pigs"; // $foo is integer (15)
?>[/code]
[url=http://www.php.net/language.types.type-juggling]...[/url]

With that said, setting a value of [url=http://www.php.net/manual/en/language.types.null.php]NULL[/url] would give the variable no type or value and therefore (unless you are explicitly working with the variable as a string) would be the preferred method.
Copy linkTweet thisAlerts:
@bokehDec 14.2006 — a variable's type is determined by the context in which that variable is used. That is to say, if you assign a string value to variable $var, $var becomes a string. If you then assign an integer value to $var, it becomes an integer.

An example of PHP's automatic type conversion is the addition operator '+'. If any of the operands is a float, then all operands are evaluated as floats, and the result will be a float.[/QUOTE]
That logic doesn't apply if the string is not empty and you try to write to it in array context.[code=php]<?php

ini_set('display_errors', 1);
error_reporting(E_ALL);

$variable = " ";

$variable[] = 1;
?>[/code]
That would raise a fatal error.

PHP does not require (or support) explicit type definition in variable declaration;[/QUOTE]It does support it![code=php]<pre>
<?php

$variable = (string)0;
var_dump($variable); # string(1) "0"

$variable = (int)0;
var_dump($variable); # int(0)

$variable = (float)0;
var_dump($variable); # float(0)

?>[/code]
Copy linkTweet thisAlerts:
@ToonMarinerDec 14.2006 — PHP does indeed support forced var type defining BUT this will NOT prevent the automagic coversion that php will perform internally when comparing two different data types.

You can of course force the datatype in comparisons - with some interesting results when they are different I might add.
×

Success!

Help @sitehatchery spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...