mmitdnn Posted April 25, 2007 Share Posted April 25, 2007 Hi guys, I want to be able to parse variables from the url into the content of a php page. For example: site.com/page.php?myvariable=something I'm currently using a very simple code (that I don't even remember) - something like: <?"myvariable"?>. This code does work on some servers but not on all. I want a more "stable" code. Something that will work on all servers no matter what php version they run. And I need something to show even if for some reason the php variable is absent. I'm not a php programmer (in case you haven't figured it out already :-)). I've been told that the string may need to have GET as well as "if else" and "echo". Could someone please tell me what code I could use? Thanks... Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/ Share on other sites More sharing options...
Fearpig Posted April 25, 2007 Share Posted April 25, 2007 Try this.... if (!isset($_GET['id'])){ $id = "YourDefaultValue"; } else { $id = $_GET['id']; } This is looking for a URL something like www.test.com?id=YourVariable If it recieves www.test.com then it uses the default value otherwise YourVariable is passed onto $id to be used later! Hope that makes some sense.... Basically you can use $id = $_GET['id']; Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-237969 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 You can also use the "Ternary Operator" (scroll down) like this <?php $id = (isset($_GET['id'])?$_GET['id']:'default value'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238011 Share on other sites More sharing options...
mmitdnn Posted April 25, 2007 Author Share Posted April 25, 2007 Hmmm... Thanks guys. I tried fearpig's code and nothing shows on the page. I tried Ken's code and I get an unexpected ";" error message Not sure if I'm doing something wrong... Thanks Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238017 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 Sorry, I left out a ")": <?php $id = (isset($_GET['id']))?$_GET['id']:'default value'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238024 Share on other sites More sharing options...
mmitdnn Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks Ken... Now the code doesn't show anything on the page. When I use my simple code <?=$id?>, it works fine. Any ideas? George Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238033 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 Please post your code between tags. Ken Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238225 Share on other sites More sharing options...
mmitdnn Posted April 25, 2007 Author Share Posted April 25, 2007 Hi Ken, <?php $id = (isset($_GET['id']))?$_GET['id']:'default value'; ?> I have essentially copied and pasted your code in the page. I also have the simple code on the page. The simple one works. The other doesn't. Thanks, George Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238269 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 No, post all of the code. Ken Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238275 Share on other sites More sharing options...
MadTechie Posted April 25, 2007 Share Posted April 25, 2007 what..!! can you post all the code Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238276 Share on other sites More sharing options...
mmitdnn Posted April 25, 2007 Author Share Posted April 25, 2007 <p>simple code: </p> <?=$id?> <p>Ken's code </p> <?php $id = (isset($_GET['id']))?$_GET['id']:'default value'; ?> This is all the code on the page. No head tags, body tags or html tags. I use hostgator.com for hosting. Thanks, George Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238301 Share on other sites More sharing options...
kenrbnsn Posted April 25, 2007 Share Posted April 25, 2007 The "<?=" is a short hand for "<?php echo". Try this: <?php $id = (isset($_GET['id']))?$_GET['id']:'default value'; echo $id; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238307 Share on other sites More sharing options...
mmitdnn Posted April 25, 2007 Author Share Posted April 25, 2007 Thanks Ken - that DID work George PS. Not sure if I should "close" this topic (or how it's done). But the issue has been resolved. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/48602-solved-simple-php-get-code/#findComment-238345 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.