Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAndres Morales
10,634 PointsWhen should I make a variable equal to a previous variable?
Why is the variable $name set to be equal to $full_name and when should I do that to other variables? As far as I understand it, the $name variable will now always be equal to $full_name, so was there ever a need for the $name variable in the first place?
1 Answer
Sean T. Unwin
28,690 PointsIn this limited example, no there isn't a need to set full_name
first, aside from being a teaching tool on how to pass one variable to another.
In a real world example say we have a registration form with a field for first name and another for last name. Once the user registers that data is submitted to a database of users. The user later logs in and gets a welcome message.
In the database, the users table has a column for each of first_name
and last_name
. To create the welcome message after a successful login the first_name
and last_name
is pulled from the database and then they are concatenated to form a full name.
Some example super simple code could look like:
// Inside a function somewhere:
// Previously established database connection
// Data is selected based upon username id
$first_name = $Database->Users['username']->FName;
$last_name = $Database->Users['username']->LName;
$full_name = $first_name . ' ' . $last_name;
$welcome_message = 'Welcome ' . $full_name;
// Send $welcome_message to the client
In this example it is recommended to use one variable for each record pulled from the database as it helps with organization as well as making changes easier later on if something changes with the application.
I hope that helps to clarify a little about the benefits of separating data between variables, as well as the benefits of combining variables when needed.
Andres Morales
10,634 PointsAndres Morales
10,634 PointsThank you for your response. I understand the importance of separating data and concatenation of strings, but what I don't understand is the reason why the variable $name is set to be equal to $full_name when you could just have first name and last name variables and then concatenate them when necessary.
Sean T. Unwin
28,690 PointsSean T. Unwin
28,690 PointsRedefining
$name
to$full_name
was just a simple example showing how to pass variables around.Combining the variables for a reasoned use (such as for needing to call the data as a group multiple times) is for efficiency and practicality. It's far easier on you, the developer, to write
$full_name
than writing$first_name . ' ' . $last_name;
in terms of time and typing as well as lowering the risk of errors, e.g. typos.