Yoda Code? PHP help pls

Marc

Well-known member
Please could someone answer this question for me. I am currently learning PHP and I am curious as to why the following does not return what I would expect:

PHP:
echo "Your name is ".$mytest->getname();

Now I expected this to return "Your name is Marc", however it actually returns "MarcYour name is "

As much as I like Yoda, please could someone let me know why this happens LOL
 
Ahhhh I see what I did

PHP:
function getName()
{
echo $this->name;
}

when what I should have done is

PHP:
function getName()
{
return $this->name;
}

Now looking at what I did wrong I would have thought it would have thrown an error .... Apparently not lol
 
Ahhhhh ..... this is because of the order of evaluation? ie. it evaluates the value of the function before it concats the string, and therefore by evaluating the function it actually echos it to the screen and since there is no return value it doesnt concat anything to the end of the main string.
 
Ahhhhh ..... this is because of the order of evaluation? ie. it evaluates the value of the function before it concats the string, and therefore by evaluating the function it actually echos it to the screen and since there is no return value it doesnt concat anything to the end of the main string.

As Jaxel has said, nearly every language evaluates from right to left using order of precedence (which can be very critical in many places). Since you have a 'string' . function();, it tries to connect them, but finds it doesn't know the value of function(), so it pulls that, and then connects the strings, then does the final far left function (echo).
 
This doesn't have anything to do with precedence and only tangentially related to order of evaluation. The function parameters are evaluated left to right, not right to left (assuming this is what you were referencing, Jaxel), although this doesn't matter here as the only function with a parameter is echo, and it only has one. The string concatenation operator is left associative. Some operators are right associative (or even non-associative).

Precedence only comes into play when talking about operators. For instance if I were to give you 5+8*2 the answer is ambiguous. It could either equal 26 or 21. However, multiplication has a higher precedence than addition, so the parser sees this as a tree (forgive the ugly ASCII tree, hopefully it displays properly):

PHP:
Correct:                Wrong:
    +                       *
   /  \                    /  \
   5   *                 +     2
      /  \             /  \
     8    2            5   8

So the correct way adds 5 to the multiplication of 8 and 2 (21). The wrong way multiplies the addition of 5 and 8 to 2 (26). This is precedence.

Order of evaluation is subtly different. Take 5-5-5 as an example. The subtraction operator is used twice, so it obviously has the same precedence level, so how do we evaluate this expression? This could either be (5-5)-5 which is -5, or 5-(5-5) which is 5. Quite a big difference. So let's construct another poor ASCII tree keeping in mind that the subtraction operator is left associative.

PHP:
Wrong:                Correct:
    -                       -
   /  \                    /  \
   5   -                 -     5
      /  \             /  \
     5    5            5   5

The subtraction operator is left associative, so it has a left leaning, or left heavy tree. So the proper way to calculate 5-5-5 would be (5-5)-5 which is -5. That is the correct way subtracts 5 from the subtraction of 5 and 5. I'll leave it as an exercise to draw 5-5-5-5. Also, assignment (=) is an example of a right associative operator.

So why go through all of this? Well this stuff depends heavily on how a language can be parsed. In the previous cases ambiguities can arise quite easily, so precedence and associativity are needed so there are no ambiguities (at least one would hope). So let's look at your example, and I'll try to keep it as simple as possible, just note that the AST would be much more complex than this in a real compiler/interpreter.

PHP:
                     echo
                      |
                      .
                     / \
              String     getName
                             |
                            echo
                             |
                           $this->name

There is no ambiguity here. Now I said that this is tangentially related to order of evaluation. This is because the concatenation operator is left associative. If it were right associative String and getName (and its children) would be swapped. However, it doesn't matter if they were swapped because it would evaluate to the same string, "MarcYour name is ". In either case it will execute the second echo first, and then finish the concatenation operation (which effectively does nothing), and then finish the first echo call. In both cases this could be rewritten as:

PHP:
function getName()
{
    echo $this->name;
}
$mytest->getName(); // Outputs "Marc"
$string = 'Your name is ' . ''; // Concatenate "Your name is" to the empty string
echo $string; // Outputs "Your name is " on the same line as the previous output
// Full Output: "MarcYour name is "

Basically this problem has more to do with how a parse tree is read and less to do with how a parse tree is constructed.

So that's why things happened the way they did. Hope that helps.
 
Cheers for that guys. Wasnt confused with the order of execution, just that it was printing it .. However thats because thats what I told it to do, and so it did that first... DOH lol ..

Some fantastic answers though and very much appreciate the time people took to respond as you have done. Another nice example of where this place hasnt got the "Google it" mentality. :)
 
Top Bottom