simply multiple $_POST value [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
if(isset($_POST["value1"]) && $_POST["value1"] != ""
&& isset($_POST["value2"]) && $_POST["value2"] != ""
&& isset($_POST["value3"]) && $_POST["value3"] != "") {
}
I send text type data using ajax and in the backend I've to validate like this. Is there any other better way to do that?

Use the empty function.
if(!empty($_POST["value1"]) && !empty($_POST["value2"]) && !empty($_POST["value3"])) {
}
PHP documentation
Returns FALSE if var exists and has a non-empty, non-zero value. Otherwise returns TRUE.
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)

You can use the empty function.

instead of isset and checking for "" you could simply use empty:
if(!empty($_POST["value1"]) && !empty($_POST["value2"])){}

Related

PHP CSV League - How can I skip null value? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

PHP Check if var is 0 or 1 and then print accordingly [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Im getting an integer from my database that is either 0 or 1. With this information i want to change a string to say Esea. This is my code which isn't working:
<?php
$esea = '';
if (!empty($final_data['esea'])) {
$esea = 'Esea'
}
?>
Then this is where i print it:
<p><?php echo $esea ?></p>
$myvalue= '';
if (!empty($final_data['esea'])) {
$myvalue= 'Foo'
} else {
$myvalue= 'Bar'
}
echo $myvalue;
If the output is Foo then $final_data['esea'] was one of:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
See the empty() documentation.
If the output is Bar then $final_data['esea'] was none of the above values.
Also see the comments here and here.
Also note that the above code can be rewritten to:
$myvalue= '';
if (empty($final_data['esea'])) {
$myvalue= 'Bar'
} else {
$myvalue= 'Foo'
}
echo $myvalue;
Which is functionally exactly the same. The only difference is emtpy(...) v.s. !empty(...) (and ofcourse the logic in the if/else swapped).

PHP Weird IF statement result on boolean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
So i have this code:
echo $argi[2][$iterate2][$iterate];
if ($argi[2][$iterate2][$iterate]==TRUE){
echo " It's true ";
}
And the result is:
true It's true
or
false It's true
Do you know why?
I tried to change IF to var=TRUE, var===TRUE, var=="true", var=1
None of these worked. I haven't got that kind of problem earlier, it's really weird...
P.S. If statement is inside two foreach loops. I don't know if that matters somehow...
EDIT
I changed echo to var_dump, and the result is:
string(5) "false" It's true
EDIT 2
Here's the part of the script:
$iterate2=0;
foreach ($UpdateData as $key => $value) {
$sql.="UPDATE `addtmptable` SET ";
$sql.="$key = CASE ";
$iterate=0;
foreach ($value as $val) {
$sql.="WHEN user = '$UpdateDataU[$iterate]' THEN $val ";
var_dump($argi[2][$iterate2][$iterate]);
if ($argi[2][$iterate2][$iterate]==true){
echo " It's true ";
}
$iterate++;
}
$sql.=";";
$iterate2++;
}
I stopped on this, i want to fire the inner foreach if the statement is true
You are testing a string against a boolean value.
The only strings that evaluate to false are "" and "0".
if (strtolower($argi[2][$iterate2][$iterate]) != "false") {
echo " It's true ";
}
A string with any value will always evaluate to boolean value true. Change your comparison to $argi[2][$iterate2][$iterate] == "TRUE" instead.

Check if atleast of the variables have a value in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have two variables in php/magento as in below
$currentA = $advert->getA();
$currentB = $advert->getB();
I want to make sure that atleast one of these have a value....Basically a validation to make sure atleast one of these have a value. Am I doing it correct?
$currentA = $advert->getA();
$currentB = $advert->getB();
if (!($currentA != '' || $currentB !== '')) {
echo "do something";
}
It is more complicated than that. Like SQL fields, php variables may also be NULL and generate warning when accessed for data.
So use empty(var) because that tests for all of the possible empty conditions and doesn't give warnings if the variable has been declared without a value.
if (!(empty($currentA) || empty($currentB))) {
echo "do something";
}
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float) "
0" (0 as a string)
NULL
FALSE
array() (an empty array)
$var; (a variable declared, but without a value)
You wouldn't need the ! because if either of these have a value, it will return true, and the ! operator checks if this condition is false, so it will work opposite of when it's supposed to. You should try
if ($currentA || $currentB) {
echo "do something";
}

What is the PHP syntax to check "is not null" or an empty string? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check if a variable is empty
Simple PHP question:
I have this stement:
if (isset($_POST["password"]) && ($_POST["password"]=="$password")) {
...//IF PASSWORD IS CORRECT STUFF WILL HAPPEN HERE
}
Somewhere above this statement I use the following line in my JavaScript to set the username as a variable both in my JavaScript and in my PHP:
uservariable = <?php $user = $_POST['user']; print ("\"" . $user . "\"")?>;
What I want to do is add a condition to make sure $user is not null or an empty string (it doesn't have to be any particular value, I just don't want it to be empty. What is the proper way to do this?
I know this is a sill question but I have no experience with PHP. Please advise, Thank you!
Null OR an empty string?
if (!empty($user)) {}
Use empty().
After realizing that $user ~= $_POST['user'] (thanks matt):
var uservariable='<?php
echo ((array_key_exists('user',$_POST)) || (!empty($_POST['user']))) ? $_POST['user'] : 'Empty Username Input';
?>';
Use empty(). It checks for both empty strings and null.
if (!empty($_POST['user'])) {
// do stuff
}
From the manual:
The following things are considered to be empty:
"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

Categories