here is a change of the values :) now check it out...

Note1 seems to behave as concatenated string at a point and at another
Point behaves as a mathematical operation.....



# $add = $obj->getNum()+1 #
# $add = 15               #

===========================

Returned Values from a Method:

---------------------------

Calculation for : '15 asdasd'.$obj->getNum()+1
 = 16
Calculation for : 'asdasd 15'.$obj->getNum()+1
 = 1
Calculation for : '15'.$obj->getNum()+1
 = 1515 <<=== NOTE1!

===========================

Variable Calculation: Get Ready for the suprise :)

---------------------------

Calculation for : '15 asdasd'.$add+1
 = 16
Calculation for : 'asdasd 15'.$add+1
 = 1
Calculation for : '15'.$add+1
 = 1516 <<=== Note 2!

===========================

Propetry Values:

---------------------------

Calculation for : '15 asdasd'.$obj->b+1
 = 16
Calculation for : 'asdasd 15'.$obj->b+1
 = 1
Calculation for : '15'.$obj->b+1
 = 1515


Below ill be pasting the source code:
Please feel free to experiment on such a behavior


=========================================

<?php
class a{
  protected $a;
        public $b;

  function a($n){
   $this->a = $n;
   $this->b = $n;
  }

  public function getNum(){
    return $this->a;
  }

}

$obj = new a(14);
$add = $obj->getNum()+1;
echo '# $add = $obj->getNum()+1 #'.PHP_EOL.'# $add = '.$add."\t\t
#".PHP_EOL;

echo PHP_EOL.'==========================='.PHP_EOL;
/** Returned values **/
echo PHP_EOL.'Returned Values from a Method:'.PHP_EOL;
echo PHP_EOL.'---------------------------'.PHP_EOL;
//takes the interger and ignores the returned value from the method
"getNum"
echo PHP_EOL.'Calculation for : \'15 asdasd\'.$obj->getNum()
+1'.PHP_EOL.' = ';
echo '15 asdasd'.$obj->getNum()+1;

//explains it now as 0 and 1 for int
echo PHP_EOL.'Calculation for : \'asdasd 15\'.$obj->getNum()
+1'.PHP_EOL.' = ';
echo 'asdasd 15'.$obj->getNum()+1;

//moreover when its just a number in a string,its interpreted as an
integer and mathematical operation works
echo PHP_EOL.'Calculation for : \'15\'.$obj->getNum()+1'.PHP_EOL.' =
';
echo '15'.$obj->getNum()+1;
echo ' <<=== NOTE1!';

echo PHP_EOL;

echo PHP_EOL.'==========================='.PHP_EOL;
/** non Returned values **/
echo PHP_EOL.'Variable Calculation: Get Ready for the
suprise :)'.PHP_EOL;
echo PHP_EOL.'---------------------------'.PHP_EOL;
//
echo PHP_EOL.'Calculation for : \'15 asdasd\'.$add+1'.PHP_EOL.' = ';
echo '15 asdasd'.$add+1;

//
echo PHP_EOL.'Calculation for : \'asdasd 15\'.$add+1'.PHP_EOL.' = ';
echo 'asdasd 15'.$add+1;

//how is this done :o amazing loool
echo PHP_EOL.'Calculation for : \'15\'.$add+1'.PHP_EOL.' = ';
echo '15'.$add+1;
echo ' <<=== Note 2!';


echo PHP_EOL;

echo PHP_EOL.'==========================='.PHP_EOL;
/** non Returned values **/
echo PHP_EOL.'Propetry Values:'.PHP_EOL;
echo PHP_EOL.'---------------------------'.PHP_EOL;
//
echo PHP_EOL.'Calculation for : \'15 asdasd\'.$obj->b+1'.PHP_EOL.' =
';
echo '15 asdasd'.$obj->b+1;

//
echo PHP_EOL.'Calculation for : \'asdasd 15\'.$obj->b+1'.PHP_EOL.' =
';
echo 'asdasd 15'.$obj->b+1;

//how is this done :o amazing loool
echo PHP_EOL.'Calculation for : \'15\'.$obj->b+1'.PHP_EOL.' = ';
echo '15'.$obj->b+1;

 ?>

=========================================



On 14 May, 12:44, Codec <[EMAIL PROTECTED]> wrote:
> Didn't expect less from your explanation Ammar :) and the values are
> as you said:
>
> $obj->getNum()  = 9;
> $add = 10; //since $add = $obj->getNum() +1
>
> On 14 May, 11:00, "Ammar Ibrahim" <[EMAIL PROTECTED]> wrote:
>
> > You forgot to supply us with the values, but anyhow, it can be derived. See
> > my answers below
>
> > On Wed, May 14, 2008 at 10:35 AM, Codec <[EMAIL PROTECTED]> wrote:
>
> > > Hey Guys,
>
> > > anyone who studied php anatomy,may explain the weired behavior.
> > > here is the output:
>
> > > # $add = $obj->getNum()+1 #
> > > # $add = 10               #
>
> > > ===========================
>
> > > Returned Values from a Method:
>
> > > ---------------------------
>
> > > Calculation for : '15 asdasd'.$obj->getNum()+1
> > >  = 16
> > > Calculation for : 'asdasd 15'.$obj->getNum()+1
> > >  = 1
> > > Calculation for : '15'.$obj->getNum()+1
> > >  = 160 <<=== NOTE1!
>
> > 1- It seems that $obj->getNum() returns (9). In the first example coercion
> > from string to int happens, and based on PHP rules, the first operand will
> > be equal to (15) this is because PHP scans the string until the number ends.
> > If it didn't start with a number, it will be 0. So 15 +1 = 16
>
> > 2- Concatenation has precedence over addition (actually, same precedence,
> > but left 
> > associativehttp://www.php.net/manual/en/language.operators.php#language.operator...)
> > , so the first operand would be 'asdasd 159', which when casted to int would
> > be 0. So: 0+1 = 1
>
> > 3- The first operand will be concated, so the results would be '159'
> > (string). But when the addition happens, coercion happens again, and it
> > would be 159 + 1
>
> > This is normal and expected behaviour so far.
>
> > > ===========================
>
> > > Variable Calculation: Get Ready for the suprise :)
>
> > > ---------------------------
>
> > > Calculation for : '15 asdasd'.$add+1
> > >  = 16
> > > Calculation for : 'asdasd 15'.$add+1
> > >  = 1
> > > Calculation for : '15'.$add+1
> > >  = 1511 <<=== Note 2!
>
> > Same as above
> > 1- The operation wouild  '15 asdasd10' + 1. The left operand when casted to
> > int would be 15, and so on
> > 2- the left operand when casted to int would be 0
> > 3- '15' . 10 is '1510' (string). Then '1510' + 1 is 1511 (int)
>
> > > ===========================
>
> > > Propetry Values:
>
> > > ---------------------------
>
> > > Calculation for : '15 asdasd'.$obj->b+1
> > >  = 16
> > > Calculation for : 'asdasd 15'.$obj->b+1
> > >  = 1
> > > Calculation for : '15'.$obj->b+1
> > >  = 160
>
> > Again, same rules as above
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Jordan PHP Users Group" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/JoPHP
http://Jolug.org/
-~----------~----~----~----~------~----~------~--~---

Reply via email to