How $_POST in php operates - php

my index file is
<body>
<form method="POST" action="post.php" >
<input name="name">
<input type="submit" value="submit">
</form>
</body>
and post.php is
<?php
$sec = 'qaswed';
if($sec == $_POST['name'])
{
echo "true";
}
else
{
echo "false";
}
?>
When I am simply writing the TRUE in place of $_POST['name'] in post.php file then irrespective of what i submit in index file,the result is true as obvious i.e
if($sec == TRUE) { echo "true"; } else { echo "false"; }//true
But, When in index file if i send TRUE in the name parameter then why the output is coming false .. i.e
if($sec == $_POST['name']) { echo "true"; } else { echo "false"; }// false when name=TRUE
when I send this from other page variable (request) then it evaluates to false.why it happens?

When you write if ($sec == TRUE), then it's true, because you are using automatic type conversion with the == operator, and php converts the $sec string to bool type, where since it's not (bool)false(not string 'false'!!!!) or (int)0, it becomes true, and the true === true = true.
If you don't want the php to automatically convert the values in the if, then use === instead of ==, which will also check the type.
In the other case you are sending a "true" string and you have "qaswed" string which is obviously not the same, and since both of them are strings there are no type conversion like in the previous case.

In first comparison value of string 'qaswed' automatically is casted to the boolean value in order to comapre to the bool. When you compare different data types one of it is casted to another type.
If you want to compare also types of the variables you should use Identical Comparison Operator.
var_dump('qaswed'); // string(6) "qaswed"
var_dump((bool)'qaswed'); // bool(true)
var_dump('qaswed' == true); // bool(true)
var_dump('qaswed' === true); // bool(false)
In second case you compare string types.
var_dump('TRUE'); // string(4) "TRUE"
var_dump('qaswed'); // string(6) "qaswed"
var_dump('qaswed' == 'TRUE'); // bool(false)
var_dump('qaswed' === 'TRUE'); // bool(false)

i just added "if (isset($_POST['name']))" for checking the name is set or not
//post.php
<?php
if (isset($_POST['name']))
{
$sec = 'qaswed';
if ($sec == $_POST['name'])
{
echo "true";
}
else
{
echo "false";
}
}
?>

Related

php check if values in form have been filled

I have a form and php that submits the values, how do I make the php to check if the values are not empty?
Server code
$XX = mysqli_real_escape_string($link, $_REQUEST['XX']);
$YY = mysqli_real_escape_string($link, $_REQUEST['YY']);
if(empty($XX) || empty($YY))
{
echo "You need to fill in XX or YY";
}
Form markup:
<form method="POST" action="">
<label for="XX">XX</label><br>
<label for="YY">YY</label><br>
<input type="text" name="XX" id="XX"><br>
<input type="text" name="YY" id="YY"><br>
<input class="button" type="submit" value="submit"><br>
</form>
Assuming you are trying to check that at least one of these inputs has been set as your echoed message suggests then you need to use an and && and not an or || like this
if(empty($XX) && empty($YY))
{
echo "You need to fill in XX or YY";
}
PHP has three useful functions to test the value of a variable, you need to understand how these functions work in order to use them properly, below is a short description for each, hope it helps
isset()
Determines if a variable is set and is NOT NULL
So if the value assigned is "" or 0 or “0” or false the return will be true, if is NULL it will return false.
$var = '';
if(isset($var)) {
echo 'The variable $var is set.';
}
unset($var);
if(!sset($var)) {
echo 'The variable $var is not set';
}
Empty()
Determines if a variable is empty
So if the value is "" or 0 or 0.0 or "0" or NULL or False or [] it will return true
$var = '';
if(empty($var)) {
echo 'The variable $var is empty or not set';
}
is_null()
It returns true only if a variable is NULL.
$var = NULL;
if(is_null($var)) {
echo 'The variable $var is NULL';
}
if(is_null($foo)) {
echo 'The variable $foo is inexistent so the value is NULL and will evaluate to true';
}

How to treat zero values as true using php?

Here is my sample code:
$issue_id = $_POST['issue_id'];
if(!empty($issue_id)){
echo 'true';
}
else{
echo 'false';
}
If I pass 0 to $_POST['issue_id'] by form submitting then it echo false. Which I want is: Condition will be true if the following conditions are fulfilled:
1. true when I pass any value having 0.
2. false when I don't pass any value. i.e: $_POST['issue_id'] is undefined.
I also tried this:
if(!isset($issue_id)){
echo 'true';
}
else{
echo 'false';
}
if(!empty($issue_id) || $issue==0){
echo 'true';
}
else{
echo 'false';
}
The last one is okay, meaning if I pass any value having ZERO then it will echo true. But it will also echo true if I don't pass any value. Any idea?
The last is okay, meaning if I pass any value having ZERO then it echo true. But it also echo true if I don't pass any value. Any idea?
if (isset($_POST["issue_id"]) && $_POST["issue_id"] !== "") {
}
please notice I used !== not !=. this is why:
0 == "" // true
0 === "" // false
See more at http://php.net/manual/en/language.operators.comparison.php
also if you are expecting number you can use
if (isset($_POST["issue_id"]) && is_numeric($_POST["issue_id"])) {
}
since is_numeric("") returns false
http://php.net/manual/en/function.is-numeric.php
Alternatively if you expect number good option is filter_var
if (isset($_POST["issue_id"]) {
$issue_id = filter_var($_POST["issue_id"], FILTER_VALIDATE_INT);
if ($issue_id !== false) {
}
}
since filter_var("", FILTER_VALIDATE_INT) will returns false and filter_var("0", FILTER_VALIDATE_INT) will return (int) 0
http://php.net/manual/en/function.filter-var.php
if(isset($_POST['issue_id'])) {
if($_POST['issue_id'] == 0) {
echo "true";
}
else {
echo "false";
}
}
When you get data from a form, remember:
All text boxes, whether input or textarea will come as strings. That includes empty text boxes, and text boxes which contain numbers.
All selected buttons will have a value, but buttons which are not selected will not be present at all. This includes radio buttons, check boxes and actual buttons.
This means that $_POST['issue_id'] will be the string '0', which is actually truthy.
If you need it to be an integer, use something like: $issue_id=intval($_POST['issue_id']);
#Abdus Sattar Bhuiyan you can also full fill your two condition like below one:
<?php
$_POST["issue_id"] = "0";
$issue_id = isset($_POST['issue_id']) ? (!empty($_POST['issue_id']) || $_POST['issue_id'] === 0 || $_POST['issue_id'] === "0") ? true : false : false;
if($issue_id){
echo 'true';
}
else{
echo 'false';
}

Strange behaviour of an IF condition

$a = 101;
if (isset($a) && is_int($a) && in_array($a, range(1, 100))) {
echo "TRUE";
} else echo "FALSE";
Why this condition returns FALSE as it should be while this IF:
if (isset($argv[1]) && is_int($argv[1]) && in_array($argv[1], range(1, 100))) {
echo "TRUE";
} else echo "FALSE";
returns also FALSE where value passed as first parameter is 50 which is in range??? PHP-CLI is 7.0.9-TS-VC14-x64
Thanks in advance
argv[1] is, by default, an string.
Use is_numeric() instead of is_int(), and convert (or cast) it to integer value before checking if in the range.
// var_dump($argv); // ...if you want to check $argv types and values.
if (isset($argv[1]) && is_numeric($argv[1]) && in_array(intval($argv[1]), range(1, 100))) {
echo "TRUE";
}
else {
echo "FALSE";
}
CAUTION: is_numeric() also returns TRUE in case of a float value!

Boolean input flipping

I'm getting really strange results on a php script that takes boolean input. The idea is that the data needs to be stored as either a 1 or a 0, but the input to the php script is a string in true/false format. Check this out:
<?php
function boolToBinary($str) {
echo $_POST['wants_sms'] . " " . $str;
die();
// posting this so that you can see what this function is supposed to do
// once it is debugged
if ($str == true) {
return 1;
} else {
return 0;
}
}
$gets_sms = boolToBinary($_POST['wants_sms']);
Here is the output from this function:
false true
How can that be??? Thanks for any advice.
EDIT: Solution: Still not sure why my output was flipped, but the fundamental problem is solved like this:
if ($str === 'true') {
return 1;
} else {
return 0;
}
Thanks to RocketHazmat for this.
http://www.php.net/manual/en/language.types.boolean.php#language.types.boolean.casting
see this example:
var_dump((bool) "false"); // bool(true)
And the explanations:
When converting to boolean, the following values are considered FALSE:
...
the empty string, and the string "0"
...
Every other value is considered TRUE (including any resource).
In your case the $_POST['wants_sms'] variable contains a string "false";

Javascript; check if a variable is 1 or 0

I'm using a variable in Javascript which will be set via Php e.g. var usesInterview = <?php echo 1;?>
If not, then var usesInterview = <?php echo 0;?>
How best should I handle this in my code? There will be a If statement to check for the variable and determine the route to take.
I've tried using typeof() == 1 and when I set it to 0, it still carries out the routine as if it where 1.
Why not set it with javascript:
usesInterview = 1;
Even if you set it with PHP, you can check like this:
if (usesInterview === 1){
// variable is equal to 1
}
else if (usesInterview === 0){
// variable is equal to 0
}
Notice the === to check for both type as well as value. If you don't want to check for type, you need to use == like this:
if (usesInterview == 1){
// variable is equal to 1 or "1" or true
}
else if (usesInterview == 0){
// variable is equal to 0 or "0" or "" or false
}
You should avoid the later approach when you are sure about both type as well as value.
More Information:
http://w3schools.com/JS/js_comparisons.asp
There are so many ways you can do it... Ie
var usesInterview = <?php echo [0|1];?>
usesInterview ? goingTrueWay() : goingFalsegWay();
or
<?php echo [0|1];?> ? goingTrueWay() : goingFalseWay();
or something like this:
var waysCollection = {
0: function () {...} //routine for usesInterview == 0
1: function () {...} //routine for usesInterview == 1
}
waysCollection[<?php echo [0|1];?>]();
also you can use one of the early suggestion:
if (<?php echo [0|1];?>) {
// truthy branch
} else {
// falsy branch
}
BTW, if you want usesInterview to be a boolean, yes/no trigger, - use true/false not 0/1. Its easier to read and understand later. For ex
var usesInterview = <?php echo [false|true];?>
if (usesInterview) {
//do this if `true`
} else {
//do this if `false`
}
typeof will return the type of the value - "number" in this case. You're using a non-strict equality check (==) so "number" == 1 is true.
Just check the value, using type-strict equality operator (===):
if (usesInterview === 1) {
// do something
}
else if (usesInterview === 0) {
// do something else
}
Read more about JavaScript comparison operators at https://developer.mozilla.org/en/JavaScript/Reference/Operators/Comparison_Operators.
When usesInterview is 1 it's truthy. So it's as simple as:
if (usesInterview) {
// truthy branch
} else {
// falsy branch
}

Categories