AffApprentice Posted November 21, 2009 Share Posted November 21, 2009 Hi guys, This is my first post, hopefully not my last! I'm trying to write a pretty straightforward piece of PHP code that does the following: 1. Tests to see if the parameter "comp" is present in the URL string. 2. If NOT, execute a javascript. If SO, do nothing. URL is something like mypage.com/example.php?comp The PHP i wrote is: <?php $comp = $_GET['comp'] if (!$comp) { echo "<script src="myscript.js" type="text/javascript"></script>"; } ?> I get a syntax error when I do this, but I'm not sure why (have tried several things including moving the "!"). Does PHP require that I set the URL parameter equal to something and test against that? So for example: mypage.com/example.php?comp=true $comp = $_GET['comp'] if (comp!=true) { ... Can't I just test for the existence of the parameter, and specify actions based on whether it's there? Help is APPRECIATED. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/182444-test-for-existence-of-parameter/ Share on other sites More sharing options...
Alex Posted November 21, 2009 Share Posted November 21, 2009 Well the parse error is probably because you're missing a ; at the end of this line: $comp = $_GET['comp'] To check if a variable is set you should use isset Ex: if(isset($_GET['comp'])) { // .. } else { // .. } Quote Link to comment https://forums.phpfreaks.com/topic/182444-test-for-existence-of-parameter/#findComment-962843 Share on other sites More sharing options...
AffApprentice Posted November 21, 2009 Author Share Posted November 21, 2009 worked like a charm, THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/182444-test-for-existence-of-parameter/#findComment-962848 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.