Jump to content

variables in variables


skaero

Recommended Posts

Hi all, I have a problem with some code I am trying to make a simple include file That I can have on all of my pages, but I can't get the $site to work so I can tell it where the site is. So how would I get this to work?

$site = 'http://site.com/';$nav = '<div id="div"><a href="#"><img src="<?php echo"$site"; ?>img/pic1.png" alt="pic1" /></a><a href="#"><img src="<?php echo"$site"; ?>img/pic2.png" alt="pic2" /></a></div>';

Thanks!

Link to comment
Share on other sites

Hi!PHP doesn't parse PHP-tags in strings, what you'll get (as output) with that code is

<div id="div"><a href="#"><img src="<?php echo"$site"; ?>img/pic1.png" alt="pic1" /></a><a href="#"><img src="<?php echo"$site"; ?>img/pic2.png" alt="pic2" /></a></div>

to include values of variables in a string there's two ways, use doubl-quotes and use the var. name directly in the string or use single quotes and concat the string.Double-quotes ("):

$nav = "<div id=\"div\"><a href=\"#\"><img src=\"$site/img/pic1.png\" alt=\"pic1\" /></a><a href=\"#\"><img src=\"$site/img/pic2.png\" alt=\"pic2\" /></a></div>";

You actually did this, in your echo: <?php echo"$site"; ?>, why I don't know, as you don't need it...And with single-wuotes ('):

$nav = '<div id="div"><a href="#"><img src="'.$site.'img/pic1.png" alt="pic1" /></a><a href="#"><img src="'.$site.'img/pic2.png" alt="pic2" /></a></div>';

psI would give the div a better Id than div ;?)dsHope that helped...Good Luck and Don't Panic!

Link to comment
Share on other sites

I don't think the $site variable is really needed. In my experience with quite a few hosts, you don't need to show the domain, just the page/image you want to link to (as long as it's on your website, not another). So eg. /index.php would work instead of mysite.com/index.php (Which would work too.) It's up to you, but I don't really think it's worth the extra time.

Link to comment
Share on other sites

To add to what Kevin says up above this post, NOT using the $site address makes the code "portable", meaning if you change domains or copy the code to another location, the link will still work since it is relatively addressed, not an absolutely address. Of course, for external links (ie:http://www.w3schools.com/), you might continue to use the http portion of the link but the links pointing to your own pages or inside your web Host, I recommend the relative address method, without the $site variable.

Link to comment
Share on other sites

Well if you were to create a folder, then from that folder have your sub folders (ie. "images")... you could use:

<img src="images/img_name.jpg" />

and then to go down a folder, for instance if you were in "website/extra/" and wanted to get an image from the image folder, you could use:

<img src="../images/img_name.jpg" />

Link to comment
Share on other sites

  • 1 month later...

Hi I have another question about variables, I what to have a php code in the variable, so that I can do a php include, and some other php code. How would I do this?Thanks for all your help!

Link to comment
Share on other sites

Hi!PHP doesn't parse PHP-tags in strings, what you'll get (as output) with that code is
<div id="div"><a href="#"><img src="<?php echo"$site"; ?>img/pic1.png" alt="pic1" /></a><a href="#"><img src="<?php echo"$site"; ?>img/pic2.png" alt="pic2" /></a></div>

Im going to have to pull you out on this one and say no, this is perfectly good code except the quotations around $site. That will still echo it out, but it will create more overhead for php.If you want to but the contents of a file into a variable through the include statement you can do it this way:
<?phpob_start();include("somefile.php");$var = ob_get_contents();ob_end_flush();?>

Thats all you have to do.

Link to comment
Share on other sites

Im going to have to pull you out on this one and say no, this is perfectly good code except the quotations around $site. That will still echo it out, but it will create more overhead for php.
You took that out of context... that code is ok, but this isn't (first post)
$nav = '<div id="div"><a href="#"><img src="<?php echo"$site"; ?>img/pic1.png" alt="pic1" /></a><a href="#"><img src="<?php echo"$site"; ?>img/pic2.png" alt="pic2" /></a></div>';

The code I had (in the quote) is what that the script would output on echo $nav;...

Thats all you have to do.
well, it's easier with file_get_contents() (just one row)Oh, well with ob you get yhe output of that code, but that could be accomplished via file_get_contents to, but then you run that script seppearately..
Link to comment
Share on other sites

If you want to execute a string of PHP code, you use the eval function.

<?php$php_code = '$var1 = 10; $var2 = $var1 * pi(); echo $var2;';eval($php_code);?>

Note that you don't need the PHP tags in the string.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...