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 trialMartins Zalans
827 PointsDifferent way to display pounds to kk
Hello.
Can I write code like this to display Miles to km in browser?
// number in miles we want to convert to kilometers
$miles = 2.5;
// floating point value for the mile to kilometer conversion
$mile_to_km = 1.60934;
// use the variable above to calculate miles multiplied by the kilometer conversion
$kilometer = $miles * $mile_to_km;
// display the miles to kilometers
echo "2.5 Miles = ($kilometer) kilometers";
3 Answers
Kristian Egebæk-Carlsen
9,809 PointsIn this case you would need some string concatenation. At the moment you are trying to echo a variable named $kilometer, but the variable name is inside the " " that you use for strings. If you want to echo that variable you have to close your string, concatenate the variable and add the rest of the string in the ind, like this:
echo "2.5 Miles = " . $kilometer . " kilometers";
David Bilson
13,819 PointsThis works! Is it ok to do it this way?
echo "Distance: {$miles} miles = {$kilometers} kilometers.";
It gives the answer:
Distance: 2.5 miles = 4.02335 kilometers.
It seems a lot more succinct than doing multiple echo statements.
Here is the same syntax shown in the php docs:
$great = 'fantastic';
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
http://php.net/manual/en/language.types.string.php#language.types.string.parsing.complex
nico dev
20,364 PointsGreat stuff. That's true. You can do that. Even better, though, you can do that without the curly braces.
That is:
echo "Distance: $miles miles = $kilometers kilometers.";
The curly braces are intended for another specific use:
There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.
Simple syntax example:
<?php
$juice = "apple";
echo "He drank some $juice juice.".PHP_EOL;
?>
Complex syntax example:
<?php
echo "This works: {$arr['key']}";
?>
In brief, and as I understand it at least, although we can use complex syntax for a simple variable, there is no need to do it.
NOTE: Regarding the multiple echo statements, it's true that is not necessary. You can use concatenation, although in this particular case I think that's not necessary either.
David Bilson
13,819 PointsGreat reply Nico and very much appreciated.
I do find, that when scanning the code, that it is very easy to spot the variables when they are enclosed within curly braces.
nico dev
20,364 Pointsnico dev
20,364 PointsJust an update note here:
I don't know about last year, and maybe you couldn't, but as of now (June '17), you can. You just need to remove the parentheses around the $kilometers variable (and make sure to use double quotes, as you did; single quotes won't make it, they will just literally echo the variable names).
Just try it! :)