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 trialAbe Layee
8,378 PointsPHP Quotation
Hello everyone. I have a question about putting quote around variable name in php. By adding quote around the varialbe name itself, shouldn't it be a string when you pass it on to the echo. In JavaScript, when you add string on the variable name, it becomes a string. For exampe
var Name = "Abraham" // global variable
function PrintName() {
if( Name ==="Abraham") {
alert("Howdy !" + Name); // alert("Howdy !" +" Name") // "Name" is now string"
} else {
alert ("Who are you?");
}
}
PrintName();
Now the PHP part.
<?php
$name = "Abraham";
function PrintName() {
global $name;
if ($name==="Abraham") {
# code...
echo "Welcome ".''."$name"; // "$name" why is this working instead of an error?
}
else{
echo "Who are you?";
}
}
PrintName();
?>
1 Answer
Colin Marshall
32,861 PointsIf you use double quotes around a variable in PHP it will not treat the variable as a string. Single quotes, will treat the variable as a string.
<?php
$name = "Colin";
echo "My name is $name"; // will print My name is Colin
echo 'My name is $name'; // will print My name is $name
?>
Abe Layee
8,378 PointsAbe Layee
8,378 PointsAww that makes sense now:D Thank you very much. I was just curious.