Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Echo Problem

  1. #1
    New Coder
    Join Date
    Jan 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Echo Problem

    I am having a problem echoing variables. Here's what I have:

    PHP Code:
    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?

  • #2
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    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

  • #3
    New Coder
    Join Date
    Jan 2005
    Posts
    21
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by BaDgEr
    There you go
    It worked perfectly. Thank you so very much.

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    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

  • #5
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    I'm the 0.000001% lol...

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


  • #6
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Taylor_1978
    I'm the 0.000001% lol...

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

    Painstaking if you have a lot of HTML to echo

  • #7
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Taylor_1978
    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

  • #9
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by delinear
    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

  • #10
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Taylor_1978
    Not if you use a PHP Editor that does all the backslashes for you! lol
    That's cheating!

  • #11
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    Quote Originally Posted by BaDgEr
    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

  • #12
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Which editor is that?

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

  • #13
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    I use PHPEdit v0.6

    You can find it at 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

  • #14
    New Coder
    Join Date
    Jul 2005
    Posts
    62
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  • #15
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    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?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •