I am in the process of creating a website using php and due to hosting my website locally and then uploading it onto a server I want an easy way of changing the root domain without manually changing it in every single place I have a domain, so I proposed storing the root domain as a variable. n.b. I have simplified down the names of the files.
I created a a variable called $rootDomain
<?php
$rootDomain = "http://localhost/testWebsite/";
?>
Next I have a new page in a sub folder called: page.php. I get to this page via a link in a header that is used for all the pages.
<a href=<?php $rootDomain?>"pageone/page.php"><li><?php echo$firstLink?></li>
Using the generic header I have on all pages I want to get back to the main index page. The link used looks like this:
<a href=<?php $rootDomain?>"index.php"><?php echo$websiteName?></a>
The expected link location should send me to:
"http://localhost/testWebsite/index.php"
But the link it is displaying is:
"http://localhost/testWebsite/pageone/index.php"
Which is an error.
When I have tested it using the link directly without the variable :
<a href="http://localhost/index.php"><?php echo$websiteName?></a>
There is no error and I get the expected outcome.
I am struggling to work out what I am doing wrong and any help would be much appreciated.
You have toecho
the variable in thehref
attribute with PHP for it to show in the HTML output.
You can do so by doing
<a href="<?php echo $rootDomain; ?>index.php"><?php echo $websiteName; ?></a>
or you can use the short echo syntax
<a href="<?= $rootDomain ?>index.php"><?= $websiteName ?></a>
You also have a couple typos in your code, like the misplaced quotes around thehref
attribute.
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.