Advanced Search

Results 1 to 10 of 10

Thread: Need help with isset function

  1. #1
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Unhappy Need help with isset function

    Hello. I'm begginer and just started to learn PHP cause before I was learning HTML and CSS, so I want to ask.
    PHP Code:
    <?
    /////////////////////////////////////////
    //// if i would write like that /////////
    ////////////////////////////////////////
    if(isset($_GET['test'])){
    echo 
    "this link would look like index.php?test";
    }

    /* However I want that my link look like ?test=0 
    I have tried write like that: */
    if(isset($_GET['test'] == $_GET['0'])) {
    echo 
    "ant it's shows for me error in line [if(isset($_GET['test'] == $_GET['0']))]";
    }
    ?>
    Anyone can help me? :S

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    isset() means 'is this variable set'.
    Just compare the variables directly-- you do not need a function:
    if ($var1==$var2) {.....}

    For the more complex approach, if you do not know if it is set, you can do this:
    if (isset($var1)&&$var1==$var2) {.....}
    ---
    also, more complicated:
    if (isset($var1)&&isset($var2)&&$var1==$var2) {.....}
    and a bit shorter:
    if (isset($var1,$var2)&&$var1==$var2) {.....}
    ---

    HOWEVER, in the URL you have this:
    page.php?variable=value

    0 is NOT a variable. It's the value.
    So $_GET['test'] IS 0, and $_GET['0'] does not exist.

    So what you want to do here is:
    if ($_GET['test']==0) {...}

    Or, even better (to avoid errors):
    if (isset($_GET['test'])&&$_GET['test']==0) {...}
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. The Following User Says Thank You to djr33 For This Useful Post:

    Total_me (04-18-2010)

  4. #3
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Till you were writted for me answer I was looking in internet for suggest and I found that:
    PHP Code:
    <?php
    switch($_get['test']){
    case 
    0: echo "test=0";
    break; 
    case 
    1: echo "test=1";
    break;
    default: echo 
    "choose link <a href='?test=0'>0</a> ... ";
    ?>
    What do you think? Is it better than do it with isset?
    What you could suggest me better cause I'm here newbie ;D

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    You should learn all of these tools. Don't be fooled into thinking that there are 'scripts' or 'ways' to do things-- there are just a lot of tools. PHP is beyond HTML and even Javascript in some ways in that there are many ways to do the same thing and some ways are better than others.
    So, no, it's not better-- it's just different.

    A switch statement is a special type of structure that is used for a single variable with many possible values. But with just a few values (especially just one value like test=0 and not test=1,2,3....) then ifs are more efficient.

    if ($x==1) {}
    if ($x==2) {}

    Same as:
    switch($x) {
    case 1:
    case 2:
    default:
    }

    [rough code]


    Again, it's completely up to you how you want to program. Read some tutorials and write something. Then you'll understand that. And then start again with something new. You'll develop your own style after a while.

    Really, programming is like art: someone can tell you how to hold the brush and what colors there are, but it's up to you to put it all together into something new and creative. It's not exactly like art, of course, but it is very similar-- there are no 'rules' about what you can make, just rules about how small pieces must go together so that everything doesn't fall apart. After that, it's your job.

    You'll learn in time.


    And again, isset() is checking to see if a variable is set-- "does variable (X) have a value?". So you could use this with ifs OR with switches. It doesn't matter. Do you need to confirm that $x has a value? Or can you continue because you know it does?
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. The Following User Says Thank You to djr33 For This Useful Post:

    Total_me (04-18-2010)

  7. #5
    Join Date
    Jul 2009
    Location
    Lithuania
    Posts
    25
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default

    Ah.. I'm really new in this one and PHP is more difficult for me than HTML or CCS cause I'm learning on my own, but I hope with dynamicdrive I'll learn faster ;]
    I think for everything need time and..
    if I use:
    PHP Code:
    if($test==0){
    echo 
    "link will look like ?test=0 ?";

    and if yes, can I use like that:
    PHP Code:
    if($_GET['Test']==0){
    echo 
    "if it's the same as if($test){}?";


  8. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    $test is a variable in PHP, like $_GET is a variable in PHP.
    When you use ['name'] that is part of an array-- it's a variable that's a set of values, like pairs: 1=>a,2=>b,3=>c.
    So $var[1]=a, $var[2]=b, etc....

    $_GET is the array from the URL. Use this to refer to any name from the URL and get its value:
    page.php?var=value
    echo $_GEt['var']; //'value'
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. The Following User Says Thank You to djr33 For This Useful Post:

    Total_me (04-18-2010)

  10. #7
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    If you want to check that $_GET['test'] is in the URL (file.php?test) then you would use:

    PHP Code:
    <?php
    if(isset($_GET['test'])
    {
      echo 
    "Test is set";
    }
    ?>
    If you want to check if $_GET['test'] has a value, then you would do:

    PHP Code:
    <?php
    if(isset($_GET['test']) && ($_GET['test'] == 'whatever_value'))
    {
      echo 
    "Do whatever here if \$_GET['test'] equals 'whatever_value'";
    }
    ?>

    There are many ways to do this, another example:

    PHP Code:

    $test 
    $_GET['test'];

    switch(
    $test)
    {
      case 
    'value_1':
         
    do_something_if_get_equals_value_1();
      break;

      default:
         
    do_something_if_get_is_empty();
      break;


    Here are some useful links:
    http://uk3.php.net/manual/en/control...res.switch.php

    http://uk.php.net/isset

    http://uk3.php.net/manual/en/reserved.variables.get.php

    Hope I helped.
    Last edited by hmsnacker123; 04-18-2010 at 06:54 PM.

  11. #8
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    262
    Thanked 690 Times in 678 Posts

    Default

    $_GET['Test']
    Warning: this is a completely different value. $_GET['test'] refers to ?test=0. $_GET['Test'] refers to ?Test=0.

    [EDIT: Post above has been corrected.]
    Last edited by djr33; 04-18-2010 at 06:56 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| espa˝ol | Deutsch | italiano | portuguŕs | catalÓ | un peu de franšais | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  12. #9
    Join Date
    Mar 2008
    Posts
    122
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Default

    Quote Originally Posted by djr33 View Post
    Warning: this is a completely different value. $_GET['test'] refers to ?test=0. $_GET['Test'] refers to ?Test=0.
    Oop.. Thank's for pointing that out, corrected.

  13. #10
    Join Date
    May 2009
    Posts
    62
    Thanks
    19
    Thanked 3 Times in 3 Posts

    Default

    Maybe this would help:

    PHP global variable: $_POST

    * $_POST is a global variable which holds the value of "index variable(s)" (this is just a name created by me to point out what I mean)
    * This "index variable", refers to the name of the html tag -- which the user puts his data. For example the textfield in html:
    Code:
    <form method="post" action="example.php">
       <input type="text" name="firstname" />
    </form>
    in this example, in my form, the method is post so that I can use the global variable $_POST. Inside my form is a textfield which name is firstname. In this case, the name of the textfield becomes an index of $_POST.
    * By the way, if you write $variable = $_POST['firstname'], $_POST['firstname'] is a value. In contrast, if you write $_POST['firstname'] = "michael" , $_POST['firstname'] is a variable. A variable in the right hand side of an equal sign is a value, while a variable in the left hand side of an equal sign is a storage.
    * I forgot, $_POST is an array while $_POST['firstname'] is a variable.


    PHP global variable: $_GET

    * Much more the same as $_POST.
    * The method of your form must be get (method="get") in order to use the global variable $_GET. (Remember: From the previous example, we use post as the value of our method because we want to use the global variable $_POST)
    * How to use $_GET:
    Code:
    <form method="get" action="example.php">
      <input type="text" name="firstname" />
    </form>
    same as $_POST, firstname will become an index of $_GET. But, unlike $_POST, you can see firstname in the url. For example, if I submitted this form, the url will be:
    Code:
    localhost/example.php?firstname=michael
    * $_GET is also used to get the variable of a url. Refer to the url above, firstname is what I mean the variable. Remember that after the question mark (?) of a url is a variable. You can make multiple variables in a url, You just add the "&" simbol after the value of the previous variable. For example:
    Code:
    localhost/example.php?firstname=michael&lastname=jordan&number=23
    firstname, lastname, and number are all variables of the url. You can use those variables as indeces of $_GET to get their values
    Code:
      $_GET['firstname']      // the value of this is michael
      $_GET['lastname']      // the value of this is jordan
      $_GET['number']        // the value of this is 23


    Refer to this site:

    www.w3schools.com
    www.thornwebdesign.de
    Last edited by heavensgate15; 04-19-2010 at 10:34 AM.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •