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
https://forums.phpfreaks.com/topic/182444-test-for-existence-of-parameter/
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
{
     // ..
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.