I'm looking to call links on my .html pages from a php file. I would like one file (currently links.php) that holds all the link information that I can call. Here is what I have created so far: links.php file $link_text = "Link 1"; $link_url = "http://www.google.com"; $link_text2 = "Link 2"; $link_url2 = "http://www.bing.com"; ?> Code (markup): index.html file Is this the best way to call links to my html pages? I am very new to php so any advice you can give me would be appreciated
What you are trying to do is 'return a variable'. Mate, time for you to go 1 step ahead and dive into Object Oriented PHP. One of the interesting php oop tut I saw was : http://www.youtube.com/watch?v=tTPW_fTJNNk Quick tutorial 11 mins but, worth it! For now: VERY BASIC would be: Links.php class Links { function linktext1() { $link_text = "Link 1"; return $link_text; } function link1() { $link_url = "http://www.google.com"; return $link_url } function linktext2() { $link_text2 = "Link 2"; return $link_text2; } function link2() { $link_url2 = "http://www.bing.com"; return $link_url2 } } PHP: then, call it using your index.php file (NOT index.html) <body> <?php include("inc/links.php"); $lin = new Links();?> <p>The search engine I most like is <a href="<?php print "$lin->linktext1()"; ?>"><?php print "$lin->link_text1"; ?></a></p> <p>I also search using <a href="<?php print "$lin->link_url2"; ?>"><?php print "$lin->link_text2"; ?></a></p> </body> PHP: You can also use GLOBALS but, I would not recommend using it wrt Security of the website. Cool enough?
Another problem you might be having is that any file with PHP executing in it has to end with .php (or use a .htaccess line for really advanced people). So, naturally, your code isn't working . If you used this: <body> <?php include("inc/links.php"); ?> <p>The search engine I most like is <a href="<?php print "$link_url"; ?>"><?php print "link_text"; ?></a></p> <p>I also search using <a href="<?php print "$link_url2"; ?>"><?php print "link_text2"; ?></a></p> </body> Code (markup): And saved it as index.php, then saved your $links file to links.php, it would work fine! Off-topic: I remember when I was in the very same position as you! I had the same setup going, too, an include with a variable in another file. Keep it up, man, one day you might become as great as I am xD (joking).
Great solution. If you php include a file from another source (let's say http://www.mysite.com/links.php), would that be usually blocked on servers? I am looking for a solution to be able to change links in a theme I am distributing, so that links are acutally called from a file on my sever. Maybe it does not even have to be a .php file, can be a .html file with a simple html code, so that people would trust it more. Is there any way to find out first if the file can be called from another server and if not, just use a static link? Thanks
Actually it's time for him to take 3 steps back and learn basic PHP before he dives into OO programming.