Jump to content

How to check if get variable is passed

- - - - -

  • Please log in to reply
4 replies to this topic

#1
thatsme

thatsme

    Programmer

  • Members
  • PipPipPipPip
  • 176 posts
Hi, i want to be able to check if variable was sent in url to php script. my code here:
(isset($_GET['subCategory']) || empty($_GET['subCategory']) || strlen(trim($_GET['subCategory'])) == 0)
Somehow I always get error Notice: Undefined index: subCategory in C:\wamp\www\IPN\private\loadArticlesList.php on line 19
Anyy tips what to do?


#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
Because in php, all the if is executed, even if the first part of the if return false
So you need to do:

if (isset($_GET['subCategory'])) 

{

  if (empty($_GET['subCategory']) || strlen(trim($_GET['subCategory'])) == 0)

  {

  ...

  }

} 



#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200

Vaielab said:

Because in php, all the if is executed, even if the first part of the if return false

That is incorrect, for example, take this clearly silly statement:

<?php
error_reporting(PHP_INT_MAX);
ini_set('display_errors', '1');

if(! isset( $knfnwjn['afsfas']) || !strlen($ossskfin) == -10 || !made_up_function($zzzzzz['zzzzz'] + 42) ) { 
  print "This line is reached with not an error.";
}
?>

It will indeed reach that print statement without error. It reminds me of a blog entry John had written a year back: Short-Circuit Evaluation in PHP | Compiled Thoughts by John Ciacia. The main concern is that PHP should try to be fast, so it is useless to execute the other statements within the IF, if it will regardless fail.

An && however will require the interpreter to parse the next statement (and fail, as $ossskfin is not a variable in the current scope.)

isset() can be very useful in this sense.

To the point: He had used || which will try to evaluate to "true", as the isset() is false, the empty() will next be performed on the non-set variable and throw the issue he has been seeing.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
I didn't know about this, I was certain that php was executing all the part of the if even if it was already false.
I'm gonna have to do some test, and probably modify some part of my code.
Thx for this info!

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
  • Location:Karlstad, Sweden
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, Pascal
  • Learning:Java, C#
This is common in many programming languages, and it's called lazy evaluation, as it don't evaluate more than needed.
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users