Here is my code
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && $_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
Please help.
Thanks.
When you do && it will evaluate all conditions until something is false. When you do || it will evaluate all conditions until something is true. Since your first conditions evaluated to the false, the 2nd one was invoked but $_POST['error'] didn't exist.
You probably want to do this, notice the brackets around your two errors.
if(
isset($_POST['error']) &&
(
$_POST['error'] == 2 ||
$_POST['error'] == 1
)
)
It can also be better re-written as.
if(
isset($_POST['error']) &&
in_array($_POST['error'], array(1,2))
)
Like Augwa said:
The && operator will evaluate all conditions until any one of them is false.
The || operator will evaluate all conditions until any one of the is true.
A solution:
if(isset($_POST['error'])) {
if($_POST['error'] != 2 && $_POST['error'] !=1) {
// Do stuff here
return true;
}else if($_POST['error'] == 2 || $_POST['error'] == 1) {
return false;
} else {
return false;
}
}
you should change your code as below... always enclose your comparison with || in brackets. because || condition checks up to last piece of code to find out a 'true' value. by re-coding as ..... && (... || .... ), the executions will return from the point && and will not execute ( .... || ..... ) part
if (isset($_POST['error']) && $_POST['error'] != 2 && $_POST['error'] != 1) {
return true;
} else if (isset($_POST['error']) && ($_POST['error'] == 2 || $_POST['error'] == 1)) {
return false;
} else {
return false;
}
Related
I have an export to excel button which downloads excel file.
However when i click it shows me error as Trying to access array offset on value of type int
My code is this:
public static function dataTypeForValue($pValue = null)
{
// Match the value against a few data types
if ($pValue === null) {
return PHPExcel_Cell_DataType::TYPE_NULL;
} elseif ($pValue === '') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ($pValue instanceof PHPExcel_RichText) {
return PHPExcel_Cell_DataType::TYPE_INLINE;
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) { //error comes in this line
return PHPExcel_Cell_DataType::TYPE_FORMULA;
} elseif (is_bool($pValue)) {
return PHPExcel_Cell_DataType::TYPE_BOOL;
} elseif (is_float($pValue) || is_int($pValue)) {
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
$tValue = ltrim($pValue, '+-');
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
return PHPExcel_Cell_DataType::TYPE_STRING;
} elseif ((strpos($pValue, '.') === false) && ($pValue > PHP_INT_MAX)) {
return PHPExcel_Cell_DataType::TYPE_STRING;
}
return PHPExcel_Cell_DataType::TYPE_NUMERIC;
} elseif (is_string($pValue) && array_key_exists($pValue, PHPExcel_Cell_DataType::getErrorCodes())) {
return PHPExcel_Cell_DataType::TYPE_ERROR;
}
return PHPExcel_Cell_DataType::TYPE_STRING;
}
}
what can be the solution for this..?
In the PHPExcel file "DefaultValueBinder.php", replace this line 82:
} elseif ($pValue[0] === '=' && strlen($pValue) > 1) {
with the following:
} elseif (0 === strpos($pValue, '=') && strlen($pValue) > 1) {
(!is_int($pValue) && $pValue[0] === '=' && strlen($pValue) > 1) should work. $pValue[0] will not work with integers so check if it is an int or not before continuing.
I update some files in library and It's work fine for me at php 7.4
Please check updated code library :
Google Drive LInk
How would the valid syntax be for the following if-statement?
if ($properties->offering_type === 'y' || $properties->offering_type === 'p' && $properties->sold != 'y') {
// echo something
} else {
}
I want to echo something when offering_type is either y or p and sold is not y
&& has higher precedence than ||, so your condition is interpreted as
if ($properties->offering_type === 'y' ||
($properties->offering_type === 'p' && $properties->sold != 'y')) {
You need to add parentheses to group the || together.
if (($properties->offering_type === 'y' || $properties->offering_type === 'p')
&& $properties->sold != 'y') {
<?php
if ( ($properties->offering_type === 'y' || $properties->offering_type === 'p') && ($properties->sold != 'y') ) {
// echo something
}
else {
}
Please note that you are using === which means the type should also be the same. And you are not doing that for the sold property (!=).
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
**I try to Exit() First If Condition, continue to next IF Condition, But Cannot Continue next IF condition totally Exit Overall Please Fix My Problem Thankyou **
No need to use else condition if you want to run second if statement
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}
}
What About the idea of this one.
<?php
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' )
{
if($var2 == 'run'){
echo 'run';
}
elseif($var3 == 'go'){
echo 'go';
}
elseif($var1 == 'clear'){
echo 'clear';
}
else {
exit();
}
}
just use only if.. use return
$var1="exit";
$var2="run";
$var3="go";
if($var1 != '' && $var2 != '' && $var3 != '' ){
//first condtion
if($var1 == 'clear'){
echo 'clear';
}else {
return;
}
//2nd condtion
if($var2 == 'run'){
echo 'run';
}else {
return;
}
//3rd condtion
if($var3 == 'go'){
echo 'go';
}else {
return;
}
}
I'm really confused, because I have an if/else statement what works at a first integration in the system, but in a second case I must rewrite the function. My opinion is, that both statements has the same logic? Or isn't it?
Statement 1: works as intended in first integration of code, but not at the second integration (always the variable ba_geschaeftszeichen has a string lenght of zero):
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
Statement 2: only this code works at the second integration:
if (
(isset($_POST['ba_geschaeftszeichen']) && ($kostentraeger == "sozialamt")) ||
(isset($_POST['pk_vnr']) && ($kostentraeger == "pflegekasse"))
) {
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0)) {
$ba_geschaeftszeichen = $_POST['ba_geschaeftszeichen'];
} else {
$ba_geschaeftszeichen = "";
}
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0)) {
$ba_geschaeftszeichen = $_POST['pk_vnr'];
} else {
$ba_geschaeftszeichen = "";
}
} else { $ba_geschaeftszeichen = ""; }
In statement 1, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
only if
if (isset($_POST['ba_geschaeftszeichen']) && (strlen($_POST['ba_geschaeftszeichen']) > 0))
is false.
In statement 2, you reach
if (isset($_POST['pk_vnr']) && (strlen($_POST['pk_vnr']) > 0))
regardless.
I have this if statment here and its not working
if(
($division->id == 1 || $division->id == 2) &&
in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships) ||
($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships))
{
return FALSE;
} else {
return TRUE;
}
What I am trying to do is say if $division is 1 or 2 and if member1, member2, member3, member4 are in the array $memberships return false, if $division is not 1 or 2 and member5 is in the array return false, everything else return true.
This is not working because member5 is in the array and $division is 1, which should return true, but it returns false.
PS - member1-5 are just names I am using for here as they actually are personal information in my array.
What Am i doing wrong?
I'd do this as a series of different 'if' statements, to avoid you going around the bend trying to work it out.
I've created this piece of code according to your statement...
What I am trying to do is say if $division is 1 or 2 and if member1,
member2, member3, member4 are in the array $memberships return false,
if $division is not 1 or 2 and member5 is in the array return false,
everything else return true.
I think I've got the logic right, I'm about to double check it:
if ($division->id == 1 || $division->id == 2)
{
if (in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_3', $memberships) || in_array('member_4', $memberships))
{
return false;
}
else
{
return true;
}
}
else
{
if (in_array("member_5", $memberships))
{
return false;
}
else
{
return true;
}
}
These are the logical groupings, 1 per line. Use parenthesis to adjust how php evaluates it if this isnt what you want.
($division->id == 1 || $division->id == 2) && in_array('member1', $memberships)
|| in_array('member2', $memberships)
|| in_array('member3', $memberships)
|| in_array('member4', $memberships)
|| ($division->id != 1 || $division->id != 2) && in_array('member5', $memberships))
Try:
if(
(($division->id == 1 || $division->id == 2) &&
(in_array('member1', $memberships) ||
in_array('member2', $memberships) ||
in_array('member3', $memberships) ||
in_array('member4', $memberships))) ||
(($division->id != 1 || $division->id != 2)
&& in_array('member5', $memberships)))
{
return FALSE;
} else {
return TRUE;
}
Less lines of code does not always mean it's better, break it down and make it readable, for example:
$output = true;
if($division->id == 1 || $division->id == 2)
{
if(in_array("member_2", $memberships) || in_array("member_3", $memberships) || in_array('member_4', $memberships))
{
$output = false;
}
}
else
{
if (in_array("member_5", $memberships))
{
$output = false;
}
}
return $output;