?
Solved

inserting variable in a link

Posted on 2011-02-14
5
Medium Priority
?
348 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 2000 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 111

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 111

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
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…
Suggested Courses

800 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