Log in

View Full Version : Need help with isset function



Total_me
04-18-2010, 07:22 AM
Hello. I'm begginer and just started to learn PHP cause before I was learning HTML and CSS, so I want to ask.


<?
/////////////////////////////////////////
//// 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

djr33
04-18-2010, 07:36 AM
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) {...}

Total_me
04-18-2010, 07:55 AM
Till you were writted for me answer I was looking in internet for suggest and I found that:


<?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

djr33
04-18-2010, 08:40 AM
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?

Total_me
04-18-2010, 10:59 AM
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:


if($test==0){
echo "link will look like ?test=0 ?";
}

and if yes, can I use like that:

if($_GET['Test']==0){
echo "if it's the same as if($test){}?";
}

djr33
04-18-2010, 11:13 AM
$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'

hmsnacker123
04-18-2010, 04:26 PM
If you want to check that $_GET['test'] is in the URL (file.php?test) then you would use:



<?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
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:




$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-structures.switch.php

http://uk.php.net/isset

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

Hope I helped. :)

djr33
04-18-2010, 06:30 PM
$_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.]

hmsnacker123
04-18-2010, 06:54 PM
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.

heavensgate15
04-19-2010, 10:26 AM
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:


<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:


<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:


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:


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


$_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