++$city where $city = ‘NY’ ?

I have found something about the unary op in PHP. I was checking it in PHP 5.2.1.
Check the following code and guess the output. When you have done check with the answer given below.

$city='NY';
echo ++$city , ' '; // think well ;)
echo $city = $city + 1, ' '; // is there any diff ?

$cg = '3.53 is my cg';
echo ++$cg, ' ';
echo $cg = $cg + 1;

Did you get your answer? then check with the following

NZ 1 3.53 is my ch 4.53

Let me explain if not matched with your answer. When php do an unary operation like (++$city) on character variable it follows perl style, its actually increment to next character, say if it is A it will B and so on… so for the example it will be NZ.
But when the same is done by using binary operation (+), in that case first it convert the string into integer (and this is called type juggling ) and in that case it converts it into 0 then do the plus operation, so the output here is 0 + 1 = 1.

So be careful what you actually want and select operator accordingly!

Cheer$

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.