crawlerbasher Posted January 13, 2010 Share Posted January 13, 2010 <php $id = strip_tags($_GET['id']); if ($id) { echo "No assigned verable or entry verable"; } elseif ($id = "Test") { echo "This is a Test Page"; } ?> Now would a if ($id) take the user to the no assigned verable if the verable in the url is empty on not asigned a verable of Test? so if index.php?id=Test takes them to this is a test page but index.php or index.php?id=gobledegoop take them to no assigned verable or entry. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/ Share on other sites More sharing options...
trq Posted January 13, 2010 Share Posted January 13, 2010 Sorry, but your question and example make little sense. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994553 Share on other sites More sharing options...
calmchess Posted January 13, 2010 Share Posted January 13, 2010 <php$id = strip_tags($_GET['id']); if ($id==null) {echo "No assigned verable or entry verable";} else {echo "This is a Test Page";}?> Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994558 Share on other sites More sharing options...
oni-kun Posted January 14, 2010 Share Posted January 14, 2010 You'd preferrably use the isset function. Here is a (shouldbe) working example: <?php if (isset($_GET['id'])) { $id = htmlentities(strip_tags($_GET['id'])); //Should really be this.. if ($id == "test") { echo "Welcome to the test page! ID = test"; } else { echo "ID is not valid, please re-enter"; //If ID = blahwhatever } } else { echo 'ID is not defined.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994571 Share on other sites More sharing options...
crawlerbasher Posted January 14, 2010 Author Share Posted January 14, 2010 Thank you, that makes sences. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994586 Share on other sites More sharing options...
bombsquad Posted January 14, 2010 Share Posted January 14, 2010 <php $id = strip_tags($_GET['id']); if ($id) { echo "No assigned verable or entry verable"; } elseif ($id = "Test") { echo "This is a Test Page"; } ?> Now would a if ($id) take the user to the no assigned verable if the verable in the url is empty on not asigned a verable of Test? so if index.php?id=Test takes them to this is a test page but index.php or index.php?id=gobledegoop take them to no assigned verable or entry. try this one: <?php $id = strip_tags($_GET['id']); if($id="test1"){ $id="test1.php"; }else{ $id="test2.php"; } ?> <div ><a href="?id=test1">menu test 1</a></div> <div ><a href="?id=test2">menu test 2</a></div> //include what page will be appear <?php require_once($id); ?> Hope it helps... Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994672 Share on other sites More sharing options...
oni-kun Posted January 14, 2010 Share Posted January 14, 2010 try this one: <?php $id = strip_tags($_GET['id']); if($id="test1"){ $id="test1.php"; }else{ $id="test2.php"; } ?> <div ><a href="?id=test1">menu test 1</a></div> <div ><a href="?id=test2">menu test 2</a></div> //include what page will be appear <?php require_once($id); ?> Hope it helps... I'd highly unrecommend using this code, as it is a direct entry into a mass amount of problems. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-994678 Share on other sites More sharing options...
x_filed2k Posted January 15, 2010 Share Posted January 15, 2010 try this one: <?php $id = strip_tags($_GET['id']); if($id="test1"){ $id="test1.php"; }else{ $id="test2.php"; } ?> <div ><a href="?id=test1">menu test 1</a></div> <div ><a href="?id=test2">menu test 2</a></div> //include what page will be appear <?php require_once($id); ?> Hope it helps... I'd highly unrecommend using this code, as it is a direct entry into a mass amount of problems. Well the codes of bombsquad and oni-kun is correct, but unnecessary. Well with the code of oni-kun, it used a long method, while with bombsquad's code he's missing one function the isset(). Let's analyze the problem: [*] Is the $_GET["id"] set? [*] If set, is the $_GET["id"] equal to Test? Now, if we make it into coding this would be the solution... if(isset($_GET["id"]) && ($id = htmlentities(strip_tags($_GET['id']))) == "Test") { echo "This is a Test Page"; } else { echo "No assigned verable or entry verable"; } Explanation: - If $_GET["id"] is not set, result would be "No assigned verable or entry verable". - If we've got set $_GET["id"] but not equal to Test, result would still be "No assigned verable or entry verable". to make it simplier. - $_GET["id"] should be set and equal to "Test". Which is equal to this condition.. if(isset($_GET["id"]) && ($id = htmlentities(strip_tags($_GET['id']))) == "Test") - any other situation that does not fall into this condition will have a result "No assigned verable or entry verable". And at the same time, we set and clean/validate the variable $id. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-995303 Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 Well with the code of oni-kun, it used a long method I'd have to say your explanation of code is hardly worth noting, as you're taking my code and compressing it into something E_STRICT would throw an warning on. Shorter = better? Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-995308 Share on other sites More sharing options...
x_filed2k Posted January 15, 2010 Share Posted January 15, 2010 I am not taking your code, but I am giving a help. I was a long method type programmer before, but i've learned a lot. Doing such long method could make the next programmer hard to understand the logic of your codes. And that is my, "The way of my programming." Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-995320 Share on other sites More sharing options...
oni-kun Posted January 15, 2010 Share Posted January 15, 2010 Doing such long method could make the next programmer hard to understand the logic of your codes. And that is my, "The way of my programming." I posted a simple, commented and proper practise of programming that is taught by the manual, you posted a compressed, shortcutted code that returns a PHP warning. How is that helpful to the OP? ..but i've learned a lot Clearly not from a good source. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-995322 Share on other sites More sharing options...
x_filed2k Posted January 15, 2010 Share Posted January 15, 2010 I before I posted my code, i've tested it first. And when i tested it above it i place a ini_set("display_errors", "1"); error_reporting(E_ALL); to make sure and to check if any of my codes has an error. And it didn't gave me any warnings or errors. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-995324 Share on other sites More sharing options...
bombsquad Posted January 21, 2010 Share Posted January 21, 2010 hahaha did I missed something here.. Maybe we all right with different approach of coding. Don't argue who is right or not. Just tell your idea and the one asking help will decide what he/she going to use to fit to his/her coding. Quote Link to comment https://forums.phpfreaks.com/topic/188394-if-statement-with-an-empty-or-unasigned-verable/#findComment-999095 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.