I have a URL like: https://website.org/withdraw.php?valid_addr=1333mwKE7EcwLaR9ztdtEt7pPEfafpW4nn&amount=0.0002&_unique=1
and A line of code that reads:
if (empty($_GET['amount']) || empty($_GET['valid_addr']) || empty($_GET['_unique']))==0) exit();
If I remove the line then the code runs successfully. Can anyone tell me what I've done wrong.
The line is supposed to stop the code from running if any of the three fields are left empty.
Thanks.
I think you want to utilize array_key_exists rather than empty.
if (!array_key_exists('amount', $_GET) ||
!array_key_exists('valid_addr', $_GET) ||
!array_key_exists('_unique', $_GET))
exit();
From PHP empty() docs
Determine whether a variable is considered to be empty. A variable is
considered empty if it does not exist or if its value equals FALSE.
empty() does not generate a warning if the variable does not exist.
See Array Key Exists docs
if (($_GET['amount'] == 0) OR ($_GET['valid_addr'] == 0) OR ($_GET['_unique'] == 0)) { exit(); }
Syntax Error. Remove the ==0) part:
if(empty($_GET['amount']) || empty($_GET['valid_addr']) || empty($_GET['_unique'])) {
exit();
}
Left parenthesis missing,
if ((empty($_GET['amount']) || empty($_GET['valid_addr']) || empty($_GET['_unique']))==0) exit();
Try it...
You're code should be something like this:
if (empty($_GET['amount']) || empty($_GET['valid_addr']) || empty($_GET['_unique']))
{
exit();
}
Above your code has syntax error. I think you want to validate unique with 0 and 1. So you should have to try this code
if (empty($_GET['amount']) || empty($_GET['valid_addr']) || $_GET['_unique'])==0) exit();
Related
I have php if statement that should display certain HTML code if two conditions are true or another two are true or third part of conditions are true.
I have several arrays - $Options_arr, $MoreOptions_arr, $Special_arr .
To explain in the easiest possible way I want to do this:
if(!empty($Options_arr[0]) && $Options_arr[0]!="") or
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") or
(!empty($Special_arr[0]) && $Special_arr[0]!="")
{?> some HTML here
All help will be appreciated thank you.
empty() already checks for empty string "" so it's shorter:
if(!empty($Options_arr[0]) || !empty($MoreOptions_arr[0]) || !empty($Special_arr[0])) {
//some HTML here
}
BragG, you can use elseif
Like:
if((!empty($Options_arr[0]) && $Options_arr[0]!="") ||
(!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") ||
(!empty($Special_arr[0]) && $Special_arr[0]!=""))
{
// some html or any code
}
I hope that is what you were looking for..
Feel free to ask any question.
You are just missing some brackets. Also || is more frequently used than OR
if((!empty($Options_arr[0]) && $Options_arr[0]!="") || (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="") || (!empty($Special_arr[0]) && $Special_arr[0]!="")){
echo '<p>hello</p>';
}
You're basically already there...
if (
(!empty($Options_arr[0]) && $Options_arr[0]!="")
|| (!empty($MoreOptions_arr[0]) && $MoreOptions_arr[0]!="")
|| (!empty($Special_arr[0]) && $Special_arr[0]!="")
){
...do something
Basically you write an if statement that resolves if any of the sub-statements are true by joining the sub-statements together with ORs
The following script should create an error when date_enabled is 2 and one of the three variables is empty. When day is empty for example, the script still doesn't echo the sentence.
Does anybody sees the problem?
$year = $_POST['date-year'];
$month = $_POST['date-month'];
$day = $_POST['date-day'];
$date_enabled = 2;
if ((($date_enabled ==2)) && ((empty($year) || empty($day) || empty($month)))){
echo "You didn't enter a valid date";
}
UPDATE - When I perform the following script it echos: its empty its empty (function). Which means that empty and the function isEmpty, which I created because of the advice of #Expert System , also works.
if (empty($day)){
echo "its empty";
}
if (!isset($day)){
echo "its not set";
}
if (isEmpty($day)){
echo "its empty (function)";
}
UPDATE AGAIN - The script above works indeed correctly. The problem lays with my form. It works fine now and thanks for your help.
I believe the definition of "empty" in your question is unclear. If empty in your question means zero-length string, then empty function is not the right one for you.
empty() definition from PHP doucment
Determine whether a variable is considered to be empty. A variable is
considered empty if it does not exist or if its value equals FALSE.
empty() does not generate a warning if the variable does not exist.
Maybe this custom function will work in your context
function isEmpty($var) {
return !isset($var) || ($var == '');
}
Then
if (($date_enabled == 2) &&
(isEmpty($year) || isEmpty($day) || isEmpty($month))){
echo "You didn't enter a valid date";
}
Try this code instead:
$year = isSet($_POST["date-year"]) ? $_POST["date-year"] : "";
$month = isSet($_POST["date-month"]) ? $_POST["date-month"] : "";
$day = isSet($_POST["date-day"]) ? $_POST["date-day"] : "";
$date_enabled = 2;
if ((($date_enabled ==2)) && ((empty($year) || empty($day) || empty($month)))){
echo "You didn't enter a valid date";
}
The problem might be the error of level E_NOTICE raised when you try to access an undefined index in $_POST array.
This shoukd notcause a problem (i.e. stop execution) with theususl/default configuration options, but with yours it might (just guessing).
I'm trying to match a condition where if the user status is 10 and ANY POST variables are not set it triggers an error:
if ($_SESSION['status']=='10' && !isset($_POST['a']) || !isset($_POST['B'])) {}
I can not use && conditions for any !isset as one variable may be set though another might not. I only want the condition to match if one or more variables are not set AND the status==10.
When testing if a $_POST variable !isset, I remove an input element from the page via a browser web tool (e.g. Firebug). When the form is submitted with the variable missing it's still passing validation incorrectly.
I am also seeking a PHP if grouping condition.
If you are looking for absolutely any PHP variables, I'd recommend this:
if (($_SESSION['status'] == 10) && (count($_POST) > 0)) {
You can then get the list of _POST var keys using array_keys($_POST).
If you are looking for a specific:
if (($_SESSION['status'] == 10) && (isset($_POST['A']) || isset($_POST['b']))) {
The order of the brackets is important. You can separate groups of logical statements with brackets.
Is that was what you were looking for?
$status = $_SESSION['status'];
if($status == '10'){
if(!isset($_POST['a']) or !isset($_POST['B'])){
//Triggers error.
}else{
//Another
}
}
Try making it a function:
function checkAllVars($dataVars, $requestVars) {
foreach($dataVars as $varname) {
if(!isset($requestVars[$varname])) {
return false;
}
}
return true;
}
$dataVars = array (
"varName1",
"varName2",
"varName3",
"varName4",
);
$allVarsSet = checkAllVars($dataVars, $_REQUEST);
you might be looking for
if($_SESSION['status']=='10' && (!isset($_POST['a']) || !isset($_POST['B']))){}
^ ^
which means if status = 10 and (if not set 'a' or not set 'B' or they can be both not set) do something
or you might be looking for
if(($_SESSION['status']=='10' && !isset($_POST['a'])) || ($_SESSION['status']=='10' && !isset($_POST['B']))){}
Hope someone can help me with this???
if (isset($_POST['frame_in']) == "yes") && (isset($_POST['collected1']) == "no") {
mail($mailTo,$subject,$message,$headers);
}
A little more info
Im trying to fire off an automated email when the following is true 'frames_in' = yes and 'collected1' = no
Your logic is wrong. You want to test if the $_POST values are set and equal to "yes" or "no". For example:
(isset($_POST['frame_in']) && $_POST['frame_in'] == "yes")
In context of your code with 2 conditions:
if ((isset($_POST['frame_in']) && $_POST['frame_in'] == "yes")
&& (isset($_POST['collected1']) && $_POST['collected1'] == "no")) {
mail($mailTo,$subject,$message,$headers);
}
Your original code was comparing the return value of isset() (boolean TRUE/FALSE) to yes or no, which it would never be.
You also had some incorrect () enclosures and a typo.
When developing, always use error_reporting(E_ALL); and ini_set('display_errors', 1); so that your syntax errors are visible on screen.
change S_POST to $_POST for a start :)
I have a problem with my php script it is...
Parse error: syntax error, unexpected T_LOGICAL_OR, expecting ')' in C:\wamp\www\register.php on line 34
I am a nube with !Empty so let me know if im doing something wrong.
if (!empty($username or $email or $password or $repassword))
{
}
else
{
}
This is my code but something in the middle of the else and if statement
So what am i doing wrong?
My question is what am i doing wrong?
oh and
$username
$email
$password
$repassword
are all values
and yes this is a script to figure out if someone has filled in all of the textboxes in the form for a register script.
Documentation Section says
empty() only checks variables as anything else will result in a parse
error. In other words, the following will not work:
empty(trim($name)).
empty is only able to check a variable for its emptiness. $username or $email or … is a boolean expression which either evaluates to TRUE or FALSE – not what you want. you have to use multiple empty calls:
if (!(empty($username) or empty($email) or empty($password) or empty($repassword))) {
// all set!
}
It sounds like you want something along these lines...
if (!empty($username) && !empty($email) && !empty($password) && !empty($repassword))
{
// none of the values are empty
}
else
{
// at least one of the values is empty
}
empty() will check whether or not a variable which is passed as its argument is considered to be empty, which means it will check whether or not its argument contains -
"" (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)
In your code, you are providing $username or $email or $password or $repassword as its argument. This is a boolean expression, meaning it will evaluate to either TRUE or FALSE. So the argument to empty() will be a boolean value, not a variable. This is the reason why you are getting this error.
I am assuming that you want to check whether or not all of these variables are empty. If this is the case, then you can do this in the following way -
if( !empty($username) or
!empty($email) or
!empty($password) or
!empty($repassword))
{
..........
}
else
{
........
}
You can't use the OR operator for calling a function multiple times with multiple variables.
If you want to make sure that all variables are not empty, use this:
if ( ! (empty($username) ||
empty($email) ||
empty($password) ||
empty($repassword)
)
)
Empty only works on variables
http://php.net/manual/en/function.empty.php
The expression x or y or z will be evaluated and therefore will not be a variable.
This won't work because empty() can only check a single variable.
Even if it did work, you are doing it wrong, this if statement would check if any of the four variables is not empty. Therefore - if it was valid code - it would return true even if only one variable was defined.
So change it to this:
if (!empty($username) and !empty($email) and !empty($password) and !empty($repassword))
You can't have multiple OR's in one empty function. Your code will have to be like this:
if (!empty($strValue1) OR !empty($strValue2) OR !empty($strValue3)) {
// code
} else {
// other code
}