Jump to content

Confusion on how variables work


Shadowing

Recommended Posts

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.
?> 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.