Results 1 to 13 of 13
  1. #1

    php echo command not working like it should.

    <?php
    $site = 'http://myarcadespot.com';

    echo '$site >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || <a href="logoout.php">Logout</a> || ';

    ?>
    I am tring to do something like the above and it only echo's the $site not http://myarcadespot.com . What am I doing wrong?

  2. #2
    echo "$site";
    ' wont cut it

  3. #3
    Single quotes are LITERAL. Try using double quotes.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  4. #4
    Join Date
    Dec 2003
    Location
    Clarksville, Indiana
    Posts
    75
    PHP Code:
    echo $site ' >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || <a href="logoout.php">Logout</a> || '
    Is what you want

  5. #5
    Join Date
    Nov 2005
    Posts
    268
    PHP Code:
    <?php
    $site 
    'http://myarcadespot.com';
     
    echo 
    "$site >> <a href=\"home.php\">Home</a> || <a href=\"login.php\">Login</a> || <a href=\"logoout.php\">Logout</a> || ";
     
    ?>
    or

    PHP Code:
    <?php
    $site 
    'http://myarcadespot.com';
     
    echo 
    "{$site} >> <a href=\"home.php\">Home</a> || <a href=\"login.php\">Login</a> || <a href=\"logoout.php\">Logout</a> || ";
     
    ?>
    or

    PHP Code:
    <?php printf("%1\$s >> <a href=\"home.php\">Home</a> || <a href=\"login.php\">Login</a> || <a href=\"logoout.php\">Logout</a> || "$site 'http://myarcadespot.com')?>
    TehBoosters line also works.
    Last edited by Korvan; 11-28-2005 at 05:05 PM.

  6. #6
    Better yet.. Dont echo static content.

    PHP Code:
    <?php
    $site 
    'http://myarcadespot.com';
    ?>
    <html>
    <body>
    <?php echo "$site";?> >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || <a href="logoout.php">Logout</a>
    </body>
    </html>
    Alternately if your php install supports short tags you can do this to output the value of $site:
    PHP Code:
    <?=$site?>
    Escaping quotes in URL strings is madness and very difficult to read / debug / modify without messing up.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  7. #7
    Join Date
    Nov 2005
    Posts
    268
    PHP Code:
    <html> 
    <body> 
    <?php echo ($site 'http://myarcadespot.com');?> >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || <a href="logoout.php">Logout</a> 
    </body> 
    </html>
    That would cut out even more php tags that are not needed. And still give you the exact same result, an echo'd string and your site name assigned to the variable $site.

  8. #8
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Alternately if your php install supports short tags you can do this to output the value of $site:
    No, no no. Do not encourage this. This is bad!!
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  9. #9
    Good point. You cannot really rely on all hosts having this setting on. My own server at home has it off, because its just LAZY.
    "The only difference between a poor person and a rich person is what they do in their spare time."
    "If youth is wasted on the young, then retirement is wasted on the old"

  10. #10
    Join Date
    Aug 2000
    Location
    Sheffield, South Yorks
    Posts
    3,480
    Quote Originally Posted by Korvan
    PHP Code:
    <html> 
    <body> 
    <?php echo ($site 'http://myarcadespot.com');?> >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || <a href="logoout.php">Logout</a> 
    </body> 
    </html>
    That would cut out even more php tags that are not needed. And still give you the exact same result, an echo'd string and your site name assigned to the variable $site.
    Not really good programming practice though, you should really initialise variables before their first use (yes, I know technically it is doing that else it'd not work).
    Karl Austin :: KDA Web Services Ltd.
    UK Business Hosting and Managed Servers - Hosting for Business Users :: 0800 5429 764
    Call us today and ask about our hosting solutions.

  11. #11
    Join Date
    Nov 2005
    Posts
    268
    writing

    1:
    echo ( $site = 'website' );

    is the exact same as writing
    2:
    $site = 'website';
    echo $site;

    The way the php engine will compute 1, is exactly like 2, it will do whats in the () first then run the function.

    If you want to initilize variables it should all be done at the start of the page. and techincally the code is initilizing and echoing the variable.

  12. #12
    Join Date
    Aug 2000
    Location
    Sheffield, South Yorks
    Posts
    3,480
    And what did I say in my post? "Yes I know technically it is doing it". As someone who has used PHP (and made a nice amount of money from it) since around v3.0.4 I am well aware of how it works. My point is, it is not good programming practice to do it like that, as it removes from the maintainability of the code - and anyone who has done any serious development work will tell you that maintenance of code is far more important than a few characters saved in the development, the cost of development fades into insignificance compared to the maintenance over the long term.
    Karl Austin :: KDA Web Services Ltd.
    UK Business Hosting and Managed Servers - Hosting for Business Users :: 0800 5429 764
    Call us today and ask about our hosting solutions.

  13. #13
    Join Date
    Nov 2005
    Posts
    268
    I did not mean to insult you but to mearly explain it out for anyone reading this thread. I did state that if you were to declare variables properly you would have to do it in the beginning of the document.

    Anyway back on topic, if you were to be real proper about this, the site name probably isnt going to change through the document so it should probably be declared as a constant.
    Define(): http://us2.php.net/manual/en/function.define.php
    Constants: http://us2.php.net/manual/en/language.constants.php
    PHP Code:
    <?php
    //define MY_SITE to the URL the site is supposed to be running on.
    if(!defined("MY_SITE")) 
      
    define("MY_SITE"'http://myarcadespot.com');
    //... more code
    ?>
    <!--start of page-->
    <!--more html-->
     
    <?php 
    echo MY_SITE ' >> <a href="home.php">Home</a> || <a href="login.php">Login</a> || ';
    ?>
    <!--more html-->
    <!--end of document-->
    Note: you can use MY_SITE interchangably with the function constant("MY_SITE")
    Last edited by Korvan; 11-30-2005 at 04:40 PM.

Posting Permissions

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