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.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Jun 2009
    Posts
    134
    Thanks
    3
    Thanked 19 Times in 19 Posts

    placeholder variables

    I have a situation upon where I have outputted some menu items that will have variable hrefs. The main block of text is going to remain the same, but the links will need to be changed lower down in the script. Is there a way to have place holder variables that I can then call to be replaced. I want to stay away from a function as the link totals are dynamic. So there could be 5 one day and 10 another.

    I have written this example php that fails due to the above problem. This is not the actual code, but if someone can explain how I would get this working, I feel I could do the rest

    PHP Code:
    $v "<a href=\"{$thevar}\">The link</a>";
    $thevar "yellow.html";

    echo 
    $v;
    // output is with empty href"" versus the wanted yellow.html 

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    I fail to see why you wouldn't want to use functions for this. It is *much* better to use them as its far more simplistic than using it without:
    PHP Code:
    function linkBuild($sLink$sText 'The link')
    {
        return 
    sprintf('<a href="%s">%s</a>'$sLink$sText);
    }
    print 
    linkBuild('yellow.html');
    print 
    linkBuild('red.html'); 
    Without a function you'll need to use eval.
    PHP Code:
    $v '<a href="$thevar">The link</a>';
    $thevar "yellow.html";
    eval(
    "echo \"" addslashes($v) . "\";"); 
    Ew. Not to mention that if $v ever contains any malicious instructions, it'll go ahead and issue those as well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Jun 2009
    Posts
    134
    Thanks
    3
    Thanked 19 Times in 19 Posts
    Thanks for your expertice, I can see your logic makes more sence than mine, so will implement the function. Report back if I get stuck, thanks


  •  

    Posting Permissions

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