prevent to input string or integer value - php

This is the PHP code and I want to check if an invalid value is present:
$response = $_POST['response'];
$visibility = $_POST['visibility'];
if($response == NULL || $visibility == NULL ){
printf("Invalid input: %s\n", mysqli_connect_error());
echo "<br/><a href='myevents.php'>Back to previous page</a>";
exit();
}
$response and $visibility should be the integer value
so If people put the string value I want to go to the if($response == NULL || $visibility == NULL ) statement.
How to write the statement $response == ???

Using is_numeric will provide the desired result. is_numeric finds whether the given variable is numeric
if (!is_numeric($response) || is_numeric($visibility))
echo "Invalid input";
Some people suggest is_int(). Don’t use is_int(). Use is_numeric() instead.
Copy the following chunk of code into a php file and run it. You’ll be surprised at the outcome:
$t = "12345";
if( is_int($t ) ) {
echo $t . " is an int!";
} else {
echo $t . " is not an int!";
}
The problem is that is_int() thinks a string of numbers is a string, not an integer.
The key difference between the two is the one checks the type of variable, is_int(), and the other checks the value of the variable, is_numeric().

Can you try using is_nan() function,
if(is_nan($response) || is_nan($visibility)){
printf("Invalid input: %s\n", mysqli_connect_error());
echo "<br/><a href='myevents.php'>Back to previous page</a>";
exit();
}
var_dump(NAN == NAN); // boolean true
var_dump(NAN === NAN); // boolean true
var_dump(is_nan(NAN)); // boolean true
var_dump(NAN == 12); // boolean true
var_dump(NAN === 12); // boolean false
var_dump(is_nan(12)); // boolean false
var_dump(NAN == 12.4); // boolean true
var_dump(NAN === 12.4); // boolean true
var_dump(is_nan(12.4)); // boolean false
var_dump(NAN == NULL); // boolean true
var_dump(NAN === NULL); // boolean false
var_dump(is_nan(NULL)); // boolean false
var_dump(NAN == 'K<WNPO'); // boolean true
var_dump(NAN === 'K<WNPO'); // boolean false
var_dump(is_nan('K<WNPO')); // null and throws a warning "Warning: is_nan() expects parameter 1 to be double, string given in NANTest.php on line 13"
Ref: http://www.php.net/manual/en/function.is-nan.php

try is_numeric or is_int
Follow these links:
http://php.net/is_int
http://www.php.net/is_numeric

Related

Does accessing a false Boolean value in $_POST return false or null?

I am sending x = false; Boolean value through the POST method. Will Request::post['x'] or $_POST['x'] return Boolean false or null?
If I try (!isset($_POST['x'])) it gives me true, but I don't understand why.
Note that using ! (the NOT logical operator), means that (!isset($_POST['x'] )) will return true if x is not set (i.e. null).
All data in $_POST is untyped; it is all a string. If you need to send Boolean values, one option would be to compare a string to "true" or "false".
if ($_POST['x'] === "true") {
// True
} elseif ($_POST['x'] === "false") {
// False
} else {
// Error - not equal to true or false string
}
A note regarding comparison operators in PHP:
$x == $y returns true if $x is equal to $y
$x === $y returns true if $x is equal to $y and they are of the same type
If $_POST['x'] will contain anything, except null, then isset() will return true.

Why is this get_post_meta returning the wrong value?

Trying to show a display notice based on the previous page, using it's post ID.
The problem is that the post meta value is stored in Wordpress as 0 (zero), but my statement is returning it as being true, when it should be false.
$previous_page = url_to_postid(wp_get_referer());
$consultationFee = null;
if(get_post_meta($previous_page, '_wp_page_template', true) == 'template-procedure-minimal.php') {
if(get_post_meta($previous_page, 'consultationFee', true) && get_post_meta($previous_page, 'consultationFee', true) === 0) {
$consultationFee = false;
} else {
$consultationFee = true;
}
}
var_dump($previous_page, get_post_meta($previous_page, 'consultationFee', true), $consultationFee);
C:\wamp64\www\bellavou\wp-content\themes\bellavou\template-request-consultation.php:11:int 3209
C:\wamp64\www\bellavou\wp-content\themes\bellavou\template-request-consultation.php:11:string '0' (length=1)
C:\wamp64\www\bellavou\wp-content\themes\bellavou\template-request-consultation.php:11:boolean true
I notice the var_dump of the value is being returned as a string. IS this right? This should be an integer. Anyway, even changing the IF statement to check against a string ie. === '0' returns the wrong value still.
What's happening?
Your code working right ( as written ).
First block:
if(get_post_meta($previous_page, 'consultationFee', true)){
}else{
//will always work this code, because '0' converted to 0( false )
}
Second block:
if(get_post_meta($previous_page, 'consultationFee', true) === 0){
}else{
//will always work this code, because ( string )'0' not equeal to ( int )0
}
get_post_meta() will return string, if last parameter $single set as true. And will return empty string ( '' ), if no value will found. So, we can't check it with isset() and empty() function. isset(get_post_meta($previous_page, 'consultationFee', true)) will always be true, and as you're expecting the return value be ( string )'0' empty(get_post_meta($previous_page, 'consultationFee', true)) will always be true.
Your $consultationFee will always be true, because:
if(get_post_meta($previous_page, 'consultationFee', true)/*returns false*/ && get_post_meta($previous_page, 'consultationFee', true) === 0/*returns false*/) {
$consultationFee = false;
} else {
//we will reach this block
$consultationFee = true;
}
if you want to compare returned value of get_post_meta with ( string )'0', use this:
if(get_post_meta($previous_page, 'consultationFee', true) === '0'))

PHP operators (! $a vs. false === $a)

What is the difference, specifically in PHP? Logically they're the same (or so seem), but is there any advantage with one over the other? Including micro-benchmarking if any difference.
Example code:
$a = fc();
// Example 1
if (!$a) echo "Ex. 1";
// Example 2
if (false === $a) echo "Ex. 2";
// Example 3
if (true !== $a) echo "Ex. 3";
function fc()
{
return false;
}
!
Just invert your result value (boolean or not) from true to false or false to true
Example:
if (!file_exists('/path/file.jpg')) {
// if file NOT exists
}
=== false (or true)
The value compared MUST BE a boolean false or true.
Example:
$name = 'Patrick Maciel';
if ($name === true) {
// not is, because "Patrick Maciel" is a String
}
BUT if you do that
if ($name == true) {
// it is! Because $name is not null
// and the value is not 'false': $name = false;
}
In this case, this operator is just for check that:
$connection = $this->database_connection_up();
if ($connection === true) {
echo 'connected to database';
} else {
echo 'error in connection';
}
$valid_credit_card = $this->validate_credit_card($information);
if ($valid_credit_card === false) {
echo 'Your credit card information is invalid'
}
!== true (or false)
It's the same thing. Only the opposite of ===, ie: the value cannot be a boolean true or false.
Sorry for my english.
The difference boils down to type juggling. The ! operator converts a value to its boolean value, then inverts that value. === false simply checks if the value is, in fact, false. If it's not false, the comparison will be false.
If the value being compared is guaranteed to be a boolean, these operations will behave identically. If the value being compared could be a non-boolean, the operations are very much different. Compare:
php > $a="0";
php > var_dump(!$a);
bool(true)
php > var_dump($a === false);
bool(false)
php > $a = false;
php > var_dump(!$a);
bool(true)
php > var_dump($a === false);
bool(true)

How to check the value and type of a unknown var

I have a variable that can be int or bool, this is because the db from where im querying it change the variable type at some point from bool to int, where now 1 is true and 0 is false.
Since php is "delicate" with the '===' i like to ask if this is the correct why to know if that var is true:
if($wallet->locked === 1 || $wallet->locked === true)
I think in this way im asking for: is the type is int and one? or is the var type bool and true?
How will you approach this problem?
Your code is the correct way.
It indeed checks if the type is integer and the value is 1, or the type is boolean and the value is true.
The expression ($x === 1 || $x === true) will be false in every other case.
If you know your variable is an integer or boolean already, and you're okay with all integers other than 0 evaluating to true, then you can just use:
if($wallet->locked) {
Which will be true whenever the above expression is, but also for values like -1, 2, 1000 or any other non-zero integer.
$wallet->locked = 1;
if($wallet->locked === true){
echo 'true';
}else{
echo 'false';
}
will produce:
false
and
$wallet->locked = 1;
if($wallet->locked == true){
echo 'true';
}else{
echo 'false';
}
will produce:
true
Let me know if that helps!
Your solution seems to be perfect, but You can also use gettype. After that You can check the return value with "integer" or "boolean". Depending on the result You can process the data the way You need it.
solution #1. If $wallet has the value of either false or 0, then PHP will not bother to check its type (because && operator is short-circuit in PHP):
$wallet = true;
//$wallet = 1;
if( $wallet && (gettype($wallet) == "integer" || gettype($wallet) == "boolean") )
{ echo "This value is either 'true and 1' OR it is '1 and an integer'"; }
else { echo "This value is not true"; }
solution #2 (depending on what You want to achieve):
$wallet = 0;
//$wallet = 1; // $wallet = 25;
//$wallet = true;
//$wallet = false;
if($wallet)
{ echo "This value is true"; }
else { echo "This value is not true"; }

Empty PHP variables

Is there a better way besides isset() or empty() to test for an empty variable?
It depends upon the context.
isset() will ONLY return true when the value of the variable is not NULL (and thereby the variable is at least defined).
empty() will return true when the value of the variable is deemed to be an "empty" value, typically this means 0, "0", NULL, FALSE, array() (an empty array) and "" (an empty string), anything else is not empty.
Some examples
FALSE == isset($foo);
TRUE == empty($foo);
$foo = NULL;
FALSE == isset($foo);
TRUE == empty($foo);
$foo = 0;
TRUE == isset($foo);
TRUE == empty($foo);
$foo = 1;
TRUE == isset($foo);
FALSE == empty($foo);
Keep an eye out for some of the strange == results you get with PHP though; you may need to use === to get the result you expect, e.g.
if (0 == '') {
echo "weird, huh?\n";
}
if (0 === '') {
echo "weird, huh?\n";
} else {
echo "that makes more sense\n";
}
Because 0 is false, and an empty string is false, 0 == '' is the same as FALSE == FALSE, which is true. Using === forces PHP to check types as well.

Categories