Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
In PHP can I capture a variable from a URL and then use that to determine what is displayed?
I am trying to avoid setting up a database for this, I know it would be better. I just want something fast and easy.
I have a page of coupon offers and I want my index.php file to display them all when no url parameters are added but when someone puts ?city=NewYork for instance I want to display only those that are relevant for New Yorkers. It seems to be working, but I am only getting the first city's HTML every time, is something missing?
My code looks like this.
<!--Start code-->
<?php
$city= $_GET['city'];
// The value of the variable name is found
echo "<h1>" . $_GET['city'] . " Offers</h1>";
if ($city = 'NewYork') {
print "New York HTML here";
} elseif ($city = 'Boston') {
print "Boston HTML here";
} elseif ($city = 'Austin') {
print "Austin HTML here";
} elseif ($city = 'WashingtonDC') {
print "WashintonDC HTML Here";
} else {
include 'directory.php';
}
?>
<!--End code-->
Directory.php is my default landing page.
5 Answers
- ChrisLv 77 years ago
You are using the assignment operator (=), not the comparison operator: == / ===.
if ($city = 'NewYork')
will always be true, because this sets $city to "NewYork", then does
if ($city), and since $city is not null or empty, it evaluates to true.
You need if ($city == "NewYork") ...
- Anonymous7 years ago
tricky stuff do a search at yahoo or google that could actually help
- 7 years ago
Remember how equality statement works. = will set it and == will compare.
Would you not be better off using a switch statement anyway?
switch($city)
case "NewYork":
//Do stuff
break;
case "Boston":
//Do stuff
break;
and so on.....
- KookiemonLv 67 years ago
Conditional equality statements use two = signs, not one.
if ($city == 'NewYork')