Shadowing Posted December 4, 2011 Share Posted December 4, 2011 After reading the PHP guide that actually seems way more useful then online tutors. Im starting to see why PHP is confusing me now. My way of thinking is way off on how the script reads stuff. when I read this code I think the output should be Bob cause echo reads $foo which equals bob and thats the end of it. but the output is actually My name is bob can someone please help me understand the error of my thinking in how this works. would be most appreciated. <?php $foo = 'Bob'; // Assign the value 'Bob' to $foo $bar = &$foo; // Reference $foo via $bar. $bar = "My name is $bar"; // Alter $bar... echo $foo; // $foo is altered too. ?> Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/ Share on other sites More sharing options...
Shadowing Posted December 4, 2011 Author Share Posted December 4, 2011 Oh I guess the way this works is cause "my name is" is in double quotes which makes it print the statement out first before the echo? Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294266 Share on other sites More sharing options...
marcelobm Posted December 4, 2011 Share Posted December 4, 2011 The reason it outputs "My name is Bob" is because when you assign $foo to $bar you are doing it with an & before the var name &$foo this means that $bar will be a reference to $foo instead of another var with foo's value, therefore any change you make to bar it's done actually for $foo. Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294268 Share on other sites More sharing options...
Shadowing Posted December 4, 2011 Author Share Posted December 4, 2011 Thanks alot marcelobm for the reply why wouldnt it echo bob first before My name is? outputing Bob My name is why does $bar = &$foo go ahead of 'Bob' Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294270 Share on other sites More sharing options...
marcelobm Posted December 4, 2011 Share Posted December 4, 2011 Because when you do $bar = "My name is $bar"; it replaces the contents of $bar, if you would like to echo Bob my name is it should be like this $bar .= "My name is"; or $bar = $bar." My name is"; Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294274 Share on other sites More sharing options...
Drongo_III Posted December 4, 2011 Share Posted December 4, 2011 Hi shadow This is quite hard to wrap your head around. The reason the order works like that can be shown with a normal variable. For instance: var1 = "TIM"; var1 = "My name is $var1"; echo var1; // Will echo "My name is TIM" I believe the reason this happens is because before the system assigns the new variable name to the second $var1 it's saying - "right, what does the value of the second var1 need to be?" And in this case its seeing a string "MY name is" followed by a variable (the first instance of $var1). So it evaluates that part of the statement - i.e. The new value for $var1 should be "My name is Tim" and then it overwrites the value held for $var1. At least that's my understanding of it... Thanks alot marcelobm for the reply why wouldnt it echo bob first before My name is? outputing Bob My name is why does $bar = &$foo go ahead of 'Bob' Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294276 Share on other sites More sharing options...
Drongo_III Posted December 4, 2011 Share Posted December 4, 2011 Hi shadow This is quite hard to wrap your head around. The reason the order works like that can be shown with a normal variable. For instance: var1 = "TIM"; var1 = "My name is $var1"; echo var1; // Will echo "My name is TIM" I believe the reason this happens is because before the system assigns the new variable name to the second $var1 it's saying - "right, what does the value of the second var1 need to be?" And in this case its seeing a string "MY name is" followed by a variable (the first instance of $var1). So it evaluates that part of the statement - i.e. The new value for $var1 should be "My name is Tim" and then it overwrites the value held for $var1. I think it's important to understand that is the way variables work and not to confuse it as a quirk of references At least that's my understanding of it... Thanks alot marcelobm for the reply why wouldnt it echo bob first before My name is? outputing Bob My name is why does $bar = &$foo go ahead of 'Bob' Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294277 Share on other sites More sharing options...
QuickOldCar Posted December 4, 2011 Share Posted December 4, 2011 or in simpler terms, every time you do = after a variable, it is now that value $bar = "Bob"; echo $bar."<br />"; $bar = "My name is $bar"; echo $bar."<br />"; $bar = 'now this is also '.$bar; echo $bar."<br />"; $bar = "Bob"; echo "now bar is just $bar again <br />"; $last = " Jones"; $bar .= $last; echo $bar; results: Bob My name is Bob now this is also My name is Bob now bar is just Bob again Bob Jones Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294278 Share on other sites More sharing options...
Shadowing Posted December 4, 2011 Author Share Posted December 4, 2011 Hey Drongo and Quickoldcar thanks for joining the conversation Yah after marcelobm got me up to speed on the basic concept i was sitting here thinking that its probably acting like a string. that was a very good way of viewing it Quickoldcar thanks for that. I really appreciate you guy's taking time with helping me improve my PHP logic Quote Link to comment https://forums.phpfreaks.com/topic/252438-confusion-on-how-variables-work/#findComment-1294285 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.