Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
icesolid
Forum Regular
Posts: 502
Joined: Mon May 06, 2002 9:36 pm
Location: Buffalo, NY

Variables

Post by icesolid »

I always use this code for checking for variables:

Code: Select all

if($var) {

} else {

}
But the other day someone told me that using this code:

Code: Select all

if($_GETї"var"]) {

} else {

}
Or this code:

Code: Select all

if(isset($_GETї"var"]) {

} else {

}
Would make my script better. Is that true?
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Re: Variables

Post by protokol »

Well, the best and most effective way to check for variables would be this:

if (strlen(trim($_GET['var'])) > 0) {

} else {

}
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

This article will explain Protokol's code:
http://www.phpcomplete.com/content.php?id=69

I knew I'd seen that somewhere recently :) .

You use $_GET when you're checking for stuff coming from the URL's query string, you wouldn't use it for checking variables you had set within the script. For info on $_GET and the other predefined variables:
http://www.php.net/manual/en/language.v ... efined.php

I prefer using either isset() or empty() (depending on what I'm looking for) to check that variables exist before I try and use them but I also trim() any user input.

Mac
Last edited by twigletmac on Fri Aug 09, 2002 1:45 am, edited 1 time in total.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Danger of cut and paste... (corrected it above too)

Mac
Post Reply