Jump to content

Data type in a form ?


blueman

Recommended Posts

Hypnos, your answer doesn't seem to be relevant to the questions...

All elements $_POST (excluding file uploads, where things start to get funky) are string variables. This includes things you'd expect to be only boolean, such as check boxes; when checked, the data will look like $_POST['checkbox']=="on".

if($var) is functionally equivalent to if($var==true) (though I believe the latter actually consumes more processor cycles, because the computer has to make two boolean checks instead of one... first it checks if $var is true, then it checks if it's true that $var is true, if that makes any sense). isset($var) returns true if a variable exists that is named $var.

[code]
<?php
error_reporting(E_ALL);
//Let's turn on error reporting because we are smart

if(isset($var)){
echo("Set");
}
else{
echo("Not Set");
}
//Will output "Set"

$var=false;
if(isset($var)){
echo("Set");
}
else{
echo("Not Set");
}
//Will output "Not Set"

if($var){
echo("True");
}
else{
echo("False");
}
//Will output "false"

unset($var);

if($var){
echo("True");
}
else{
echo("False");
}
/*Will cause an "Undefined Variable" error...
but would print "False" if we were dumb and didn't turn on full error reporting*/
?>[/code]
Link to comment
https://forums.phpfreaks.com/topic/30860-data-type-in-a-form/#findComment-142315
Share on other sites

The question was changed. The original question as I read it was how to tell if a var was a string or number.

[quote author=blueman link=topic=118855.msg485996#msg485996 date=1166255244]
and, one more question: is there a difference between [color=red]if($_POST["variable"])

}[/color]

and

[color=red]if(isset($_POST["variable"]){

}[/color]
[/quote]
There is a difference between if(isset($var)) and if($var). In my experence isset is more sensitive to throw a true.

Here is an example.
[code=php:0]
<?php
$a = "";

if($a) print "if(\$a) says \$a is set. \n";
else print "if(\$a) says \$a is not set. \n";

if(isset($a)) print "isset(\$a) says \$a is set. \n";
else print "isset(\$a) says \$a is not set. \n";
?>
[/code]

It will output:
if($a) says $a is not set.
isset($a) says $a is set.
Link to comment
https://forums.phpfreaks.com/topic/30860-data-type-in-a-form/#findComment-142586
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.