i know to about "isset" in php

Discussion in 'PHP' started by n.sudera87, Apr 24, 2013.

  1. #1
    hi friend , what is isset and how to use this
     
    n.sudera87, Apr 24, 2013 IP
  2. bc1

    bc1 Active Member

    Messages:
    45
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    93
    #2
    isset is a function you can use to test if a variable is set or not.

    if (isset($something)) {
    //if the variable '$something' has a value set this code will run
    } else {
    //or if you didn't set '$something' yet this code will run
    }

    Setting a variable just means giving it a value like this:

    $something = 123;

    The manual explains it better than I can and is available in many languages ;)
     
    bc1, Apr 24, 2013 IP
  3. nhl4000

    nhl4000 Well-Known Member

    Messages:
    479
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    110
    #3
    nhl4000, Apr 25, 2013 IP
    sarahk and Devtard like this.
  4. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #4
    Isset as mentioned above is to check if a variable is set or not hence is-set/isset.

    It works particularly well for form submissions to check if the user actually pressed the submit button. We do this to make sure to:

    A. Prevent a rogue user posting their own variables through the process system
    B. Prevent displaying irrelevant information to the user if they accidentally land on that page

    We can use the isset in this way like:

    if (ISSET($_POST['Submit'])){
     
    echo "The form has been submitted";
     
    //post some items from the form
     
    echo "Username: ".$_POST['username']."<br />Password: ".$_POST['password']."";
     
    }
    PHP:
    This would post items from the form.

    To make a website dynamic, we can use the isset function to grab an ID from the URL and select the corresponding record from the database like:


    if (ISSET($_GET['id'])){
     
    echo "The ID in the url is ".$_GET['id']."";
     
    //select item from the database using the ID collected from the URL
     
     
    $sth = $dbh->prepare("SELECT username FROM users WHERE id = :id");
    $sth->execute(array(":id"=>$_GET['id']));
     
       while ($row = $sth->fetch()){
              echo "The user's name is: ".$row['username']."";
        } else {
                   echo "There is no user";
                  }
    PHP:
     
    scottlpool2003, Apr 25, 2013 IP