aarontbode Posted July 31, 2017 Share Posted July 31, 2017 I'm a newby to PHP. Let's get that out of the way first. Here's what I'm looking to do: <a href="<?php include("../file.php"); ?>">Text here</a> My goal is to have a URL placed on the file.php document, so instead of having to update each instance of that link individually I can just update the document and streamline the whole process. How can I achieve this? Is this possible, and what method should I be using? Thanks! Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 31, 2017 Share Posted July 31, 2017 You could. For example, you could put an echo statement in file.php which displays the URL. Then when you include() file.php, as you have in the code above, the URL would be added to the href attribute in the source code. For what it's worth, you would probably be better off storing the common URLs in a database. Having them scattered in a bunch of PHP files seems like a potential maintenance nightmare. Quote Link to comment Share on other sites More sharing options...
requinix Posted July 31, 2017 Share Posted July 31, 2017 As cyberRobot said you certainly can do it (and easily) but that's a highly irregular approach. What is the problem that you think having the URL in a PHP script will solve? Quote Link to comment Share on other sites More sharing options...
aarontbode Posted July 31, 2017 Author Share Posted July 31, 2017 (edited) Thank you both for your replies. What would you recommend (code snippets would be highly appreciated)? The URL will need to be updated frequently across multiple files, so my goal is to be able to hop in, update the link, and have it reflect across multiple files. Currently, I have to go in and update each link manually, which isn't the end of the world but I'm trying to determine a more efficient way of updating the links. Edited July 31, 2017 by aarontbode Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 31, 2017 Share Posted July 31, 2017 It might help to know more information on the project. As requinix mentioned, "What is the problem that you think having the URL in a PHP script will solve?" Are these links primarily internal links (links to the same website / links you control) or external links? Also could you clarify what you mean by frequently changing URLs. I guess I haven't had any issues with links changing that often. It can feel that way, since there always seems to be a website that changes their domain name or decides to restructure their content. But I don't think I've needed to update the same link tag within the same year before. Are you hosting a library of links? If so, is there other data associated with the links, such as page / website name? I would lean towards using a database for storing connected data like this. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 31, 2017 Share Posted July 31, 2017 (edited) Creating a script to produce a single URL is nonsense, so that's out of question. Surely you have an application-wide configuration. If you don't, now is the time to create one. Put the URL into the configuration, then simply reference it whenever you need the URL. <a href="<?= html_escape(config_get('foo_url'), 'UTF-8') ?>">text</a> Edited July 31, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.