Solved

inserting variable in a link

Posted on 2011-02-14
5
345 Views
Last Modified: 2012-05-11
This should be a very easy question for you php ninjas.

I want to create a variable in my webpage, like this?

<%php i=100; %>

<a href="http://www.test.com/?=<%php i %>" >link 100</a>

What is the exact code to declare the variable and use it in a link so that the resulting link is http://www.test.com/?=100

Thanks!
0
Comment
Question by:keks_
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 34888633
Something along these lines assuming that $i = 100

<a href="http://www.test.com/?=<?php echo $i; ?>" >link 100</a>
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 34888636
Actually, if you want the URL as the text of the link as well then


<a href="http://www.test.com/?=<?php echo $i; ?>" >http://www.test.com/?=<?php echo $i; ?></a>

0
 
LVL 13

Expert Comment

by:darren-w-
ID: 34888888
or you can create a function:
<?php

function createHyperlink($path, $text) {
    return "<a href = '$path'>$text</a>";
}
?>

<?php echo createHyperlink("http://www.google.co.uk", "A link to google"); ?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34889401
Some introductory help with PHP is available here: http://us.php.net/tut.php

Another way of doing this is to use HEREDOC notation.  I like HEREDOC because it helps me keep logic and presentation separated.


// UNTESTED CODE BUT VALID IN PRINCIPLE

// SET VALUE FOR ARGUMENT IN STRING NOTATION
$i = '100';

// ALL URL VARIABLES MUST BE URLENCODED
$url_i = urlencode($i);

// DECLARE THE LINK USING HEREDOC
$link = <<<LINK
<a href="http://www.test.com/?=$url_i" >link 100</a>
LINK;

// SHOW THE LINK
echo $link;

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34889421
Sidebar note.  You might want to try that URL something more like this...
<a href="http://www.test.com?q=$url_i" >

In your test.com/index.php script you will find a variable named $_GET["q"] with a value equal to the contents of $url_i
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question