Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I tired to use isset($queryvar) but that didnt work, so I implemented my own function qv_isset, but it runs into a problem when $ch is set to 0.

$cls = get_query_var('cls');
$ch = get_query_var('ch');

function qv_isset($var) {
    if($var == '') { return false; 
    } else { return true; }
}

if(qv_isset($cls) && qv_isset($ch)) {
    mcs_textbook_chapter($dialect, $cls, $ch);
} elseif(qv_isset($cls)) {              
    mcs_textbook_chapter($dialect, $cls);
} else {
    mcs_textbook($dialect);
}
share|improve this question
    
Right, it's not trivial to properly check whether or not a query var has been set. I've provided a patch for wordpress which should make that more easy, probably this gets some traction: Ticket #16471. –  hakre Feb 6 '11 at 16:00
add comment

4 Answers

use the indentical comparison operator:

if(get_query_var('cls') !== '' && get_query_var('ch') !== '') {
    mcs_textbook_chapter($dialect, $cls, $ch);
} elseif(get_query_var('cls') !== '') {              
    mcs_textbook_chapter($dialect, $cls);
} else {
    mcs_textbook($dialect);
}
share|improve this answer
2  
That works too. –  MikeSchinkel Jan 29 '11 at 21:43
add comment

You'll want to use empty, isset failed because you just set the variable. An empty value is still a value.

if ( empty($cls) && empty($ch) ){
    // Not set
}
share|improve this answer
1  
I read up on the empty() function and found out 0 is considered to be empty. How would I instead be able to use 0 in the link: http://localhost/wordpress/textbook/cantonese/CK1/0/ cantonese/cls/ch/ –  imHavoc Jan 29 '11 at 0:34
    
@imHavoc - I've posted an answer that takes care of the shortcomings of empty. –  hakre Feb 6 '11 at 15:37
add comment

Hi @imHavoc:

I might write that code like this (although there are several valid ways to write it):

$cls = get_query_var('cls');
$ch =  get_query_var('ch');

if (strlen("{$cls}{$ch}")==0) {
  mcs_textbook($dialect);
} if (strlen($ch)==0) {
  mcs_textbook_chapter($dialect, $cls);
} else {
  mcs_textbook_chapter($dialect, $cls, $ch);
}
share|improve this answer
    
when $ch = 0; the thing fires off, but echo $ch displays nothing. –  imHavoc Jan 29 '11 at 21:32
    
@imHavoc - I'm confused, how does echo $ch relate to your question or the example in my answer? –  MikeSchinkel Jan 29 '11 at 21:40
    
@Mike - Sorry for the confusion, let me explain. echo $ch is just being used to debug, to find out what is being passed on to the function. When $ch = 0;, echo $ch; returns nothing, so nothing is passed onto the function. –  imHavoc Jan 30 '11 at 15:33
    
@imHavoc - What I am confused by is how you know that $ch = 0 if echo $ch; displays nothing? it would seem that would be impossible so I am expecting you are assuming something that I am not aware of. –  MikeSchinkel Jan 31 '11 at 2:42
1  
@imHavoc - Ah, that is completely different from saying ch = 0; is true. :) Have you defined 'ch' as a valid query argument using $GLOBALS['wp']->add_query_var('ch') in an 'init' hook? Also, I'd really recommend longer, more descriptive names than cls and ch if for no other reason than longer more descriptive names are less likely to conflict with names people set in plugins because most plugin vendors do not follow best practices. –  MikeSchinkel Jan 31 '11 at 3:18
show 2 more comments

If you really want to know wether a WordPress Query Var is set or not, you need to check directly for it.

All set query vars are stored into a global accessible array of the WP_Query class.

It has a default instance that is bound to the global $wp_query variable. With that in mind it is possible to create a qv_isset() function that is working precicesly:

function qv_isset($var_name) {
    $array = $GLOBALS['wp_query']->query_vars;
    return array_key_exists($var_name, $array);
}

What makes this so easy is that array_key_exists() returns true or false.

Normally to test any PHP variable if it is set or not, you can use isset(). But it has a shortcoming: If a variable is NULL, it will say that it is not set - even if it is. Comparable to what has been written about empty() here as well, it will say comparably for '' (empty string), NULL, 0 etc. .

Anyway, we do not need to rely on that function to check wether or not a queryvar has been set as we can make use of array_key_exists() here.


Now for your code:

function qv_get($var_name, $default = null) {
    return qv_isset($var_name) ? get_query_var($var_name) : $default;
}

mcs_textbook_chapter($dialect, qv_get('cls'), qv_get('ch'));

Or with other words: If your function already has (that many) optional parameters, take a look how you can best feed it. Unset variables are NULL (like said above with isset()), so this is useful to pass them as unset optional parameters as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.