Log in

View Full Version : help, I've got a hard time of understanding this one....



heavensgate15
05-30-2009, 01:33 PM
what is the difference between these two ECHO statements:

$test = "30cm";
$newtest = $test + "40cm";

//first echo version
echo "Your imaginary box has a width of $newtest centimeters";

//second echo version
echo "your imaginary box has a width of " . $newtest . " centimeters";

OUTPUT:
Your imaginary box has a width of 70 centimetersYour imaginary box has a width of 70 centimeters

it seems like their output is the same..... but when I added "cm" in the first echo version:

//fist echo version
echo "Your imaginary box has a width of $newtestcm centimeters";

this is what the browser displayed:

OUTPUT:
Your imaginary box has a width of centimetersyour imaginary box has a width of 70 centimeters



The number 70 was not displayed....I don't understand it... Also, the statement of the first version of echo, I write it in a manner that it is executed as one string, so I expected that instead of printing 70, it will print $newtest, but it doesn't seem to be that way...... Can somebody explain to me this?

thetestingsite
05-30-2009, 01:42 PM
One this line:



echo "Your imaginary box has a width of $newtestcm centimeters";


the variable $newtestcm is not defined.

Hope this helps.

heavensgate15
05-30-2009, 01:50 PM
I also think about that... but what bothers me is that, is it possible to echo multiple characters together with a variable in just one string quote? what I mean is this:

echo "Your imaginary box has a width of $test centimeters";



What if I wanted to display $test as a string, not a variable:

OUTPUT:
your imaginary box has a width of $test centimeters

thetestingsite
05-30-2009, 03:11 PM
use single quotes then. PHP 101:

The following code:



$str = 'TEST';
echo "This is a $str";


will output:



This is a TEST


where as this:



$str = 'TEST';
echo 'This is a $str';


will output:



This is a $str


Hope this helps.

heavensgate15
05-30-2009, 11:52 PM
Thanks, you really help a lot......:)