5.5 Easiest Way to Check ISSET?

Discussion in 'PHP' started by scottlpool2003, Feb 20, 2014.

  1. #1
    
    if (isset($_POST['catid']) && ($_POST['catid'] == 1) || (isset($_POST['catid']) && ($_POST['catid'] == 4))){
    
    }
    
    PHP:
    Is there an easier way of doing this other than turning off error reporting for this as some stuff gets really repetitive and long winded:

    
    if (isset($_POST['fname']) && ($_POST['fname']) && (isset($_POST['fname']) && ($_POST['sname']) && (isset($_POST['email']) && ($_POST['email']) && (isset($_POST['pass']) && ($_POST['pass']) && (isset($_POST['postcode']) &&  ($_POST['postcode']) && (isset($_POST['agerange']) &&  ($_POST['agerange']))))))) {
    
    PHP:
    I thought maybe I could put $_POST into a class and check them all at once but that didn't work.

    Any ideas?
     
    scottlpool2003, Feb 20, 2014 IP
  2. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #2
    firstly don't trust any users input. always validate and clean. so here would be my way of doing this.

    
    if (isset($_POST['cat'])) {
        $catid = (int)$_POST['cat'];
    }
    else {
        $catid = 0;
    }
    
    if (in_array($catid,array(1,4))) {
    }
    
    PHP:
    or if you end up adding more catid's

    
    if (isset($_POST['cat'])) {
        $catid = (int)$_POST['cat'];
    }
    else {
        $catid = 0;
    }
    
    switch ($catid) {
         case 1:
         case 4:
    // simply add more id's
    //  case 6:
    //  case 19:
             // code you want to run goes here
             break;
         default :
           // if you want to run any code if cat id is not 1 or 4
    }
    
    PHP:
    keep the code clean simple and easy readable.
     
    stephan2307, Feb 20, 2014 IP
  3. stephan2307

    stephan2307 Well-Known Member

    Messages:
    1,277
    Likes Received:
    33
    Best Answers:
    7
    Trophy Points:
    150
    #3
    you could also do this if you wanted to do it rally short

    
    if ( isset($_POST['cat']) && in_array( (int)$_POST['cat'], array(1,4) ) ) {
    }
    
    PHP:
     
    stephan2307, Feb 20, 2014 IP
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    How would you handle a long one like this though?

    if (isset($_POST['fname']) && ($_POST['fname']) && (isset($_POST['fname']) && ($_POST['sname']) && (isset($_POST['email']) && ($_POST['email']) && (isset($_POST['pass']) && ($_POST['pass']) && (isset($_POST['postcode']) && ($_POST['postcode']) && (isset($_POST['agerange']) && ($_POST['agerange']))))))) {
    PHP:
     
    scottlpool2003, Feb 20, 2014 IP
  5. Webox

    Webox Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #5
    I recommed to create function for this, like this (off course you can edit by self):

    function postValue($NAME) {
          
            if(isset($_POST[$NAME]) && !empty($_POST[$NAME])) {
              
                return mysql_real_escape_string(htmlspecialchars($_POST[$NAME]));
          
            }
      
        }
      
      
        function getValue($NAME) {
          
            if(isset($_GET[$NAME]) && !empty($_GET[$NAME])) {
              
                return mysql_real_escape_string(htmlspecialchars(urldecode($_GET[$NAME])));
          
            }
      
        }
    PHP:

    And use it:
    if(postValue("post_name")) {
    #YES
    } else {
      #NOT
    }
    PHP:
     
    Webox, Feb 20, 2014 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Note that isset() can take multiple arguments.
    
    if (isset($_POST['foo'], $_POST['bar'], ...))
    {
        // ... all set
    }
    
    PHP:
     
    nico_swd, Feb 20, 2014 IP
  7. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #7
    You should use !empty() instead of isset(). empty() checks that it is set and not NULL. isset() allows it to be empty.
     
    DomainerHelper, Feb 20, 2014 IP
  8. DomainerHelper

    DomainerHelper Well-Known Member

    Messages:
    445
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    100
    #8
    A function for checking them? Are you joking? You create functions to reuse code over and over. Creating a function is overkill.

    Now OP asks for the "easiest" way. "easiest" way makes for crappy code. Do you want the easiest way or the smartest way?

    <?php
    if (!function_exists('sqlclean')) {
        function sqlclean($data) {
            if (filter_var($data, FILTER_VALIDATE_INT) !== FALSE) {
                return $data;
            } else {
                $data = urldecode($data);
                $data = filter_var($data, FILTER_SANITIZE_STRING);
                return mysql_real_escape_string($data);
            }
        }
    }
    if (!empty($_POST['catid'])){
        if ($_POST['catid'] == 1 || $_POST['catid'] == 4) {
            echo 'cat id is either 1 or 4';exit;
        } else {
            echo 'cat id is neither 1 nor 4.';exit;
        }
    } else {
        echo 'no cat id sent';exit;
    }
    $required = array('fname','sname','email','pass','postcode','agerange');
    foreach ($required AS $req) {
        if (empty($_POST[$req])) {
            switch($req) {
                case 'fname':
                    echo 'You left the first name blank.';
                break;
                case 'sname':
                    echo 'You left the last name blank.';
                break;
                case 'email':
                    echo 'You left the email blank.';
                break;
                case 'password':
                    echo 'You left the password blank.';
                break;
                case 'postcode':
                    echo 'You left the postal code blank.';
                break;
                case 'agerange':
                    echo 'You left the age range blank.';
                break;
            }
             exit;
        } else {
            $$req = sqlclean($_POST[$req]);
        }
    }
    ?>
    PHP:
     
    Last edited: Feb 20, 2014
    DomainerHelper, Feb 20, 2014 IP