Jump to content

Test for existence of parameter?


AffApprentice

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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
{
     // ..
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.