Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am trying to condition if one of these has null value, then else but i am getting error:
ErrorException (E_NOTICE)
Undefined variable: average_winning_trade
DD(winning_trades_profit) result : []
here is the code for condition:
if(!empty($losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades)) {
$average_winning_trade = round(array_sum($winning_trades_profit) / $profitable_trades);
$average_losing_trade = array_sum($losing_trades_loss) / $losing_trades;
$payoff_ratio_per_trade = abs(round($average_winning_trade / $average_losing_trade, 2));
}else { $payoff_ratio_per_trade ="0";}
Your problem is in your if condition:
if(!empty($losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades))
because $losing_trades_loss && $profitable_trades && $winning_trades_profit && $losing_trades is a boolean expression, it will always return true or false, and so empty() will always return false, so regardless of the state of those variables, the first part of your if statement will execute.
I think what you actually want is
if (isset($losing_trades_loss, $profitable_trades, $winning_trades_profit, $losing_trades))
or possibly
if (isset($losing_trades, $profitable_trades) && count($winning_trades_profit) && count($losing_trades_loss))
without seeing the rest of your code it's difficult to say which of the above would be correct.
Edit
You probably also need to change your assignments to check for 0 divisors:
$average_winning_trade = $profitable_trades ? round(array_sum($winning_trades_profit) / $profitable_trades) : 0;
$average_losing_trade = $losing_trades ? array_sum($losing_trades_loss) / $losing_trades : 0;
$payoff_ratio_per_trade = $average_losing_trade ? abs(round($average_winning_trade / $average_losing_trade, 2)) : 1;
As your error code shows
ErrorException (E_NOTICE) Undefined variable: average_winning_trade
Meaning you are trying to call the variable somewhere else in your code while it hasn't been set yet. This could be either before or after you reach the if-statement you put here. However, this if-statement is not causing the error to appear.
UPDATE
Your current if-statement is as follow:
IF NOT EMPTY $losing_trades_loss AND $profitable_trades AND $Winning_trades
but you clearly want
IF NOT EMPTY $losing_trades_loss OR $poritable_trades OR $winning_trades
so update your if-statement as follow:
if(!empty(
$losing_trades_loss ||
$profitable_trades ||
$winning_trades_profit ||
$losing_trades)) {
$average_winning_trade = round(array_sum($winning_trades_profit) / $profitable_trades);
$average_losing_trade = array_sum($losing_trades_loss) / $losing_trades;
$payoff_ratio_per_trade = abs(round($average_winning_trade / $average_losing_trade, 2));
}else { $payoff_ratio_per_trade ="0";}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Take these levels:
level1 = 0days
level2 = 30days
level3 = 90days
All these levels are totally dynamic. Admin can alter number of levels and duration of levels at any time.
I am recording numberOfDailyLogin in a series, so if user not login even for 1 day meter reset to 0.
I am trying to give user a special level tag on basis of numberOfDailyLogin
I have a solution already by running a while loop from 0 to numberOfDailyLogin and update variable once >= daysRequired for login.
But I don't want it this way, want something easy
I am totally clueless at this point, I can't hardcode any of the
value, so please if you are interest please give some suggestion.
#Edit1
// json response for levels
{
"status": 200,
"body": [
{
"name": "level1",
"after_days": 0,
},
{
"name": "level2",
"after_days": 50,
},
{
"name": "level3",
"after_days": 180,
}
]
}
function getLevel($allLevels, $numberOfDailyLogins) {
foreach ($allLevels as $l) {
if ($numberOfDailyLogins > $l['after_days']) {
return $l['name'];
}
}
}
Problem is if numberOfDailyLogins=51.. so it should show level2... cause after 50 days userLevel reached to level2.. but my code return level1 which is totally wrong.
#brombeer suggestion works for me, Thanks mate
function getLevel($allLevels, $numberOfDailyLogins) {
$level = null;
foreach ($allLevels as $l) {
if ($numberOfDailyLogins> $l['after_days']) {
$level = $l;
}
}
// TO-DO... do whatever you want
}
It's a simple logic error - your code returns after the first iteration because the value is already greater than 0 (the first level in the list).
One simple solution is to search the array in reverse instead:
function getLevel($allLevels, $numberOfDailyLogins) {
for ($i = count($allLevels) -1; $i >= 0; $i--) {
if ($numberOfDailyLogins > $allLevels[$i]['after_days']) {
return $allLevels[$i]['name'];
}
}
}
Demo: http://sandbox.onlinephpfunctions.com/code/3b00c6d182676411915cae1fbb21a1afba2548e8
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am missing something. I'm sure is something stupid but I don't know what it is, sorry.
I am assinging a string to $onClick using a ternary operator.
Right after that, I try to access $onClick and I get Undefined variable: onClick error.
I have tried with simpler versions (booleans instead of strings) but it's the same. I also tried wrapping them with same result.
Apart from the example below (CakePHP), I created an online snippet here replacing debug() with var_dump(). Note: to see notices on this console you have to fork it, but I assure you there are notices!
Note: trying to provide as much examples as I can, I may have complicated things. The line I have problems with is the one commented with this: <--- this one
Code:
$id = 39; // just for this example
$isNew = true; // just for this example
// debug() examples:
debug($isNew); // line 42
debug($isNew ? "sendMeasure('add', null)" : "sendMeasure('edit', {$id})"); //42
// Actual problematic line:
$onCLick = $isNew ? "sendMeasure('add', null)" : "sendMeasure('edit', {$id})"; // <--- this one
debug($onClick); // line 47
// tying a simple bool value (just in case):
$onCLick = $isNew ? true : false;
debug($onClick); // line 51
// I even wrapped them (just in case):
$onCLick = ($isNew ? true : false);
debug($onClick); // line 55
// Expected behavior below:
if ($isNew) {
$onClick = "sendMeasure('add', null)";
} else {
$onClick = "sendMeasure('edit', {$id})";
}
debug($onClick); // line 63
Output:
Issue with uppercase and lowercase variable. variable defined as $onCLick and you checking as $onClick
// Actual problematic line:
$onClick = $isNew ? "sendMeasure('add', null)" : "sendMeasure('edit', {$id})"; // <--- this one
debug($onClick); // line 47
// tying a simple bool value (just in case):
$onClick = $isNew ? true : false;
debug($onClick); // line 51
// I even wrapped them (just in case):
$onClick = ($isNew ? true : false);
debug($onClick); // line 55
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I set the following PHP Code:
if($motherFirst == 'yes')
{$motherBool = true;}
else
{$motherBool = false;}
if($fatherFirst == 'no')
{$fatherBool = true;}
else
{$fatherBool = false;}
I have checked that I'm obtaining Yes or No as a value for both $motherFirst and $fatherFirst by doing an echo.
I am then using $motherBool and $fatherBool to post boolean (true or false) not as string in an API call.
However it appears that they are being assigned true or false of type string. What can I do to convert them to Boolean?
Above has been corrected, but now have issue with following:
I'm also sending the json as follows:
$data_string = '{
"motherData": false,
"fatherData": true
}';
Which works, but when I put as follows, it doesn't work:
$data_string = '{
"motherData": '.$motherBool.',
"fatherData": '.$fatherBool.'
}';
Can you let me know?
Already answered but here is a simplification.
$motherBool = ($motherFirst == 'yes');
$fatherBool = ($fatherFirst == 'no');
In your second question you're echoing a bool as if its a string which will print 0 or 1, not true/false as your expecting. You need to convert the bool to a string. for example,
$data_string = '{
"motherData": '.($motherBool ? 'true' : 'false').',
"fatherData": '.($fatherBool ? 'true' : 'false').'
}';
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm having a little problem with validation. I have here this code snippet that works by this way: if the pizza_size variable is not set in the Query String and the ID of product category is 1 (corresponds to pizzas) or this is set but the pizza_size value is not equal to small, medium or big this needs to return to the page products.php with the message that the size is not selected. I'm trying with:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" || $_GET['pizza_size'] != "medium" || $_GET['pizza_size'] != "big" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
else //Add the product
The problem is that this is not working. Even if the pizza_size corresponds to small, medium or big I receive the error message. But if I use only one comparison, this works great, like:
if(!isset($_GET['pizza_size']) || $_GET['pizza_size'] != "small" && $category_id == 1)
{
redirect_to('products.php?message=no-size-informed');
}
But I need to validate with the 3 sizes. How to do this validation with all the three possible variables? Thanks!
if(($category_id == 1)
&& !(isset($_GET['pizza_size']) && in_array($_GET['pizza_size'], array('small', 'medium', 'big')))
) {
redirect_to('products.php?mesage=no-size-informed');
}
else {
// Add the product
}
You have to do it like this:
if(($category_id == 1)
&& (isset($_GET['pizza_size'] && !in_array($_GET['pizza_size'], array('small', 'medium', 'big')) || !isset($_GET['pizza_size']
) {
redirect_to('products.php?mesage=no-size-informed');
}
I have this code :
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];}
I read about this error on this site but i have no ideea how to apply the fix on my particular code. I`m sorry if i repost the problem.
Try doing: var_dump($userdata->yim); to verify if yim does in fact exist and contains the key 'text'.
Or even just var_dump($userdata);
$userdata->yim['text'] is not set, so you should check for that first:
if(isset($userdata->yim['text']) && strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']) ){
However, if that section of code relies on that value, something is wrong before that and this will just hide that problem.
In order to debug this you will need to utilize print_r() so that you can see the contents of your object or array:
// see what $userdata contains - update your question with the results of the line below
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata, true).'</pre></div>';
// see what $userdata->yim contains - update your question with the results of the line below as well
echo '<div style="background-color:white; padding:15px;"><pre>'.print_r($userdata->yim, true).'</pre></div>';
if(strlen($userdata->yim['text']) > 2 && !isset($_POST['step1']))
{
$GLOBALS['error'] = 1;
$GLOBALS['error_type'] = "tip";
$GLOBALS['error_msg'] = $userdata->yim['text'];
}