...

View Full Version : Echo Problem



dudeman0501
07-10-2005, 08:40 PM
I am having a problem echoing variables. Here's what I have:



echo '<p><font face="Arial">Item added successfully.</font></p>
<b><font face="Arial">Item Preview:</font></b>
<h2>$item_title</h2>

<p><font face="Arial">$item_descr</font></p>
<h3>Price: </h3>
<p><font face="Arial">$item_price</font></p>
<font face="Arial" size="2">$item_number</font><p></p>
<img border="0" src="http://www.backwoodsparts.com/images/line.jpg" width="705" height="3"><p>';


Instead of outputting the contents of the variable, it outputs the variable name itself. What have I missed this time?

BaDgEr
07-10-2005, 08:58 PM
echo '<p><font face="Arial">Item added successfully.</font></p>
<b><font face="Arial">Item Preview:</font></b>
<h2>'.$item_title.'</h2>

<p><font face="Arial">'.$item_descr.'</font></p>
<h3>Price: </h3>
<p><font face="Arial">'.$item_price.'</font></p>
<font face="Arial" size="2">'.$item_number.'</font><p></p>
<img border="0" src="http://www.backwoodsparts.com/images/line.jpg" width="705" height="3"><p>';


There you go :)

dudeman0501
07-10-2005, 09:00 PM
There you go :)

It worked perfectly. Thank you so very much.

Nightfire
07-11-2005, 12:55 AM
Variables inside single quotes ( ' ' ), print off the variable as you've noticed. If you have the variable in the echo which was with double quotes ( " " ) then the variable is parsed.

The method BaDgEr has shown is the preferred way of 99.999999% of people

Taylor_1978
07-11-2005, 01:01 AM
I'm the 0.000001% lol...

I always do mine "<body bgcolor=\"$color\">"

:)

BaDgEr
07-11-2005, 01:03 AM
I'm the 0.000001% lol...

I always do mine "<body bgcolor=\"$color\">"

:)

Painstaking if you have a lot of HTML to echo :)

Taylor_1978
07-11-2005, 08:10 AM
Not if you use a PHP Editor that does all the backslashes for you! lol

And when I am using Notepad, doing this:

echo "<body bgcolor=\"$color\">";

Is really not much different to:

echo "<body bgcolor'.$color.'>";

Infact, its exactly the same amount of keystrokes. :)

delinear
07-11-2005, 09:23 AM
And when I am using Notepad, doing this:

echo "<body bgcolor=\"$color\">";

Is really not much different to:

echo "<body bgcolor'.$color.'>";

Infact, its exactly the same amount of keystrokes. :)

Actually, it's quite a bit different. It might be the same number of keystrokes, but in terms of efficiency it's much better (particularly in large chunks of html) to use single quotes and close the quote while you echo the variable than it is to use double quotes around the whole thing. The reason for this is that PHP will parse the whole of the string in double quotes looking for variables, so if you have a big chunk of html and one variable buried in there somewhere then it's hugely inefficient to have PHP parse the whole lot. Of course, on smaller bits of code it's not an issue, but as projects grow this kind of thing can buy you back some lost efficiency and make your apps a little faster :)

Taylor_1978
07-11-2005, 11:27 AM
Actually, it's quite a bit different. It might be the same number of keystrokes, but in terms of efficiency it's much better (particularly in large chunks of html) to use single quotes and close the quote while you echo the variable than it is to use double quotes around the whole thing. The reason for this is that PHP will parse the whole of the string in double quotes looking for variables, so if you have a big chunk of html and one variable buried in there somewhere then it's hugely inefficient to have PHP parse the whole lot. Of course, on smaller bits of code it's not an issue, but as projects grow this kind of thing can buy you back some lost efficiency and make your apps a little faster :)

That makes sense.. Hmm... now to change my habit.. they die hard!! lol

BaDgEr
07-11-2005, 03:25 PM
Not if you use a PHP Editor that does all the backslashes for you! lol



That's cheating! :)

Taylor_1978
07-11-2005, 03:29 PM
That's cheating! :)

You bet!!!!!! lol

But I also call it smart... I'd prefer to use an editor that will do all my opening/closing brackets and quotes etc and show me I'm missing a bracket rather than using notepad and spend 2 hours trying to find where a missing character should be placed!

Time is money!!! lol :)

BaDgEr
07-11-2005, 04:02 PM
Which editor is that? :o

I've tried a few, but I prefer using Dreamweaver because of the ability to save directly to my FTP via ctrl+s.

Taylor_1978
07-11-2005, 04:30 PM
I use PHPEdit v0.6

You can find it at www.waterproof.fr (http://www.waterproof.fr)

Has a free version which has a couple of limitations I'm sure, although I don't know what! lol

It does lack FTP so I just use FTP commander for that :)

BaDgEr
07-12-2005, 12:30 AM
Bah I'll stick to dreamweaver.. if I had to FTP the files into place all the time I'd forget where I was up to with the code :p

Taylor_1978
07-12-2005, 01:16 AM
Okay... 1 problem that does bring up.. I can't use: \n inside single quotes as it just prints them... is there a fix for that?

Kurashu
07-12-2005, 01:45 AM
$string = 'This is my string with a new line' . "\r\n";

$string = 'This is on its own line.
This should be as well.';



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum