Importing variable into URL [PHP]

Soldato
Joined
25 Oct 2006
Posts
5,395
Say I have a variable named $variable how can I import it into a URL link e.g. www.site.com/$variable

I tried with this but got an error.

PHP:
<body>
<?php 
$variable = "mypage.html";

<iframe width=100% height=100% scrolling=no style="border:0" src="http://site.com/$variable"></iframe> <br /> 

?> 

</body>

Error I got:
Code:
Parse error: syntax error, unexpected '<' in /home/lethal/public_html/mibbit.php on line 12

I'm new to PHP and have barely any experience with it, I guess this is probably quite simple.

Thanks for any help :)
 
put an echo before the <iframe> tag as you want php to output it as html and as its within php tags it wont be read as html which is why your getting an error. You'll need a ; at the end of the line to.
 
Ok I tried with this but still got the error.
PHP:
<body>
<?php 
$variable = "mypage.php";

echo <iframe width=100% height=100% scrolling=no style="border:0" src="http://www.site.com/$variable"></iframe> <br /> ;

?> 

</body>
 
here's a couple of different ways of doing this. :p

PHP:
<?php 
$variable = "mypage.php";
?> 

<body>
<iframe width=100% height=100% scrolling=no style="border:0" src="http://www.site.com/<?php echo $variable; ?>"></iframe> <br />
</body>

or

PHP:
<body>
<?php 
$variable = "mypage.php";
echo '<iframe width=100% height=100% scrolling=no style="border:0" src="http://www.site.com/' . $variable . '"></iframe> <br />';
?> 
</body>

(note the single quotes around what you echo)
 
PHP:
<body>
<?php 
$variable = "mypage.php";
?>

<iframe width=100% height=100% scrolling=no style="border:0" src="http://www.site.com/<?php echo $variable; ?>"></iframe> <br />

</body>

Try that :)
 
Last edited:
IIRC, you can replace this:

Code:
<?php echo $variable; ?>

with

Code:
<?=$variable ?>

It's easier to type :)
 
marc's code will work....

GeForce you have an extra ?> in your initial code mate... ;)

From what i've read recently about PHP it's best not to use the shorthand for the reasons as marc and Moredhel are stating.

Why an iframe by the way? I know i myself block a lot of content that loads via an iframe, ads etc....

If you need something inside a set area you don't need to use frames/iframes.

What is the use for the said content?
 
Last edited:
Oops. :o

Cheers for pointing that out :)

;)

Embedding a mibbit widget into a page.

http://wiki.mibbit.com/index.php/Widget

What would be a better way to do it? I just used what they suggested.

Well it really depends how you want it to sit with your page. I just find iframes are one of the many things people will naturally block along with popup javascript windows. Not so friendly with the XHTML compliancy either.

Ah i see, it's because you're loading content from a remote site (assuming you're using it the same manner as on the wiki page).

I'd personally use the object tag instead, as also shown on that wiki page.

If you're loading the content from your own website, there's no reason you need to use object or iframe, just a simple include would be sufficient i would have thought.
 
Back
Top Bottom