Tag Archives: Scripting languages

PHP Strings Don’t Need Quotes

I bet you didn’t know that PHP strings don’t need quotes! Indeed PHP developers work with strings with either single or double quotes, but actually in some cases you don’t need them.

PHP by Book

Here’s how PHP developer declare a string, which is something very common in any programming language.

$my_var = 'hello world';
// or
$my_var = "hello world";

PHP Tricks

What if you do the following:

echo hello;

That appears to be correct … Well, it’s not absolutely correct. You’ll be “noticed”.

// Notice: Use of undefined constant hello
echo hello;

However if you disable error reporting, the code will be completely fine.

error_reporting(0);
 
// no problem now
echo hello;

Variations

What follows from the thing above is that you can use strings without quotes:

// hello
echo hello;
 
// hello world (concatenated)
echo hello . ' world';
 
// helloworld
echo hello . world;

However you can’t have spaces and most of the “special” symbols.

// syntax error
echo hello world;
 
// syntax error
echo hello!;

Final Words

Although you can do this in PHP, that is completely wrong. The code becomes more difficult to read and understand. In the second place you can miss a $ sign in front of a variable declaration and thus the PHP interpreter will assume this is a string. So disable error reporting isn’t so great sometimes.

PHP: Don’t Call the Destructor Explicitly

“PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++”[1] says the documentation for destructors, but let’s see the following class.

class A
{
	public function __construct()
	{
		echo 'building an object';
	}
 
	public function __destruct()
	{
		echo 'destroying the object';
	}
}
 
$obj = new A();

Well, as you can not call the constructor explicitly:

$obj->__construct();

So we should not call the destructor explicitly:

$obj->__destruct();

The problem is that I’ve seen this many times, but it’s a pity that this won’t destroy the object and it is still a valid PHP code.
Continue reading PHP: Don’t Call the Destructor Explicitly

Object Cloning and Passing by Reference in PHP

In PHP everything’s a reference! I’ve heard it so many times in my practice. No, these words are too strong! Let’s see some examples.

Passing by reference in PHP can be tricky!
Some developers think that everything's passed by reference in PHP.

Passing Parameters by Reference

Clearly when we pass parameters to a function it’s not by reference. How to check this? Well, like this.

function f($param)
{
	$param++;
}
 
$a = 5;
f($a);
 
echo $a;

Now the value of $a equals 5. If it were passed by reference, it would be 6. With a little change of the code we can get it.

function f(&$param)
{
	$param++;
}
 
$a = 5;
f($a);
 
echo $a;

Now the variable’s value is 6.

So far, so good. Now what about copying objects?
Continue reading Object Cloning and Passing by Reference in PHP

Does JavaScript undefined Equals undefined?

Weird JS

Does "undefined" equals "undefined"?
Does JavaScript "undefined" equals "undefined"?

As you know sometimes JavaScript can be weird. Let’s see the following example and let’s try to answer the question: does “undefined” equals “undefined”. What do I mean?

First take a look at the following code.

var a;
var b = undefined;
 
// alerts "false"
alert(b == a);

Both a and b are undefined, but they are NOT equal. Now let’s see where it can become a problem.

We have an object with one member variable that is not defined.

var f1 = function()
{
	this.myvar;
};
 
var obj1 = new f1();

Now you’d like to know whether the object “b” has the property “myvar”. There are lots of examples online, but what’s the right way?
Continue reading Does JavaScript undefined Equals undefined?