PHP if condition fails inside a foreach loop - php

<?php
$key='APS';
$value='A|B|';
if ($key == 'APS'){
$aps = $key;
if (!empty($value)){
if(preg_match("/\|/",$value)){
$elephant = explode('|',$value);
foreach ($elephant as $elekey=>$elevalue){
if($elevalue = 'A'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'B'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'C'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'D'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
if($elevalue = 'E'){
$elevalue_a=$elevalue;
if(isset($aps) && ($aps != '')){
if(isset($elevalue_a) && ($elevalue_a != '')){
echo $elevalue;
echo '<br>';
}
}
}
}
}
else{
echo $singlevalue = $value;
}
}
else {
echo $value='NIL';
}
}
?>
The above code may be lengthy but it's a very simple example, where if you execute you can see the if conditions will be failing to escape the loops.
Why in PHP if condition fails inside a foreach loop?

The Problem : You are using assignment operator inside if statements
Replace = with == or === (strict check)
if($elevalue = 'B'){
to
if($elevalue == 'B'){ [or] if($elevalue === 'B'){

Related

How to Exit If Statement and Run Another IF Statement?

$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;
}
}

Which is equivalent for PHP Include at CodeIgniter3?

Please explain to me how to do it:
<?php
if(!isset($_GET['s'])) {
$_GET['s'] = ""; }
if ($_GET['s'] == "") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "1") { include("Templates/Tutorial/1.tpl"); }
if ($_GET['s'] == "2") { include("Templates/Tutorial/2.tpl"); }
if ($_GET['s'] == "3") { include("Templates/Tutorial/3.tpl"); }
if ($_GET['s'] == "4") { include("Templates/Tutorial/4.tpl"); }
if ($_GET['s'] == "5") { include("Templates/Tutorial/5.tpl"); }
?>
Thank you very much!
I think this will also work
<?php
$inc = 1;
if(isset($_GET['s']) && $_GET['s'] != ""):
$inc = (int)$_GET['s'];
endif;
include("Templates/Tutorial/$inc.tpl");
?>

Dynamic Conditional String

I have the code below taken partially inside a function:
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = '($row >= $from) && ($row <= $to)';
}else if(($from != '')&&($to == '')){
$dynamic_comparison = '($row >= $from)';
}else if(($from == '')&&($to != '')){
$dynamic_comparison = '($row <= $to)';
}
$form, $to and $row are the parameters of the function.
I want to evaluate $dynamic_comparison into something like this:
if($dynamic_comparison){
//A bunch of code here...
}
I tried:
if(eval($dynamic_comparison)){
//A bunch of code here...
}
It throws an error. How to get this right?
eval() is bad idea imho.
You can use anonymous function, like this
$dynamic_comparison = function($row, $to, $from) { return false };
if(($from == '')&&($to == '')){
$dynamic_comparison = function($row, $to, $from) { return true;};
}else if(($from != '')&&($to != '')){
$dynamic_comparison = function($row, $to, $from) { return ($row >= $from) && ($row <= $to);};
}else if(($from != '')&&($to == '')){
$dynamic_comparison = function($row, $to, $from) { return ($row >= $from);};
}else if(($from == '')&&($to != '')){
$dynamic_comparison = function($row, $to, $from) { return ($row <= $to);};
}
And use it like
if($dynamic_comparison($row, $to, $from))...
If you want to use eval anyway:
$dynamic_comparison = 'return false;';
if(($from == '')&&($to == '')){
$dynamic_comparison = 'return true;';
}else if(($from != '')&&($to != '')){
$dynamic_comparison = 'return ($row >= $from) && ($row <= $to);';
}else if(($from != '')&&($to == '')){
$dynamic_comparison = 'return ($row >= $from);';
}else if(($from == '')&&($to != '')){
$dynamic_comparison = 'return ($row <= $to);';
}
if(eval($dynamic_comparison)){//$to,$from,$row must be available in this scope
i think you have to use double quotes insted of single quotes for your string like this :
<?php
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = "($row >= $from) && ($row <= $to)";
}else if(($from != '')&&($to == '')){
$dynamic_comparison = "($row >= $from)";
}else if(($from == '')&&($to != '')){
$dynamic_comparison = "($row <= $to)";
}
if you don't have $row initialezed at this moment , escape $ character :
<?php
$dynamic_comparison = '';
if(($from == '')&&($to == '')){
$dynamic_comparison = 1;
}else if(($from != '')&&($to != '')){
$dynamic_comparison = "(\$row >= $from) && (\$row <= $to)";
}else if(($from != '')&&($to == '')){
$dynamic_comparison = "(\$row >= $from)";
}else if(($from == '')&&($to != '')){
$dynamic_comparison = "(\$row <= $to)";
}
see eval() documentation : http://php.net/manual/en/function.eval.php

Need help to add second condition to existing code

I am trying to add a second condition to existing code but it doesn't seem to be working.
The conditions are:
Compare two strings, from different arrays (working)
And check the value of a third string from a different array (not
working)
Here is the working code without the second condition: http://pastebin.com/bfpNb9zw
Here is my attempt:
Basically, the bit I am trying to get working is this part && ($ca = '') && ($ca = '0') && ($ca = '1') but it seems $ca is not able to be read outside the loop
if(!function_exists('lookup')){
function lookup($chain, $type) {
$cacount = count($chain['tbsCertificate']['extensions']);
for($j = 0; $j < $cacount; $j++) {
$count = count($chain['tbsCertificate'][$type]['rdnSequence']);
$exists = array('utf8String', 'printableString', 'teletexString', 'bmpString', 'universalString', 'ia5String');
$oid = array('id-at-commonName');
for($i = 0; $i < $count; $i++) {
foreach($exists as $field) {
if(
array_key_exists($field, $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value']) &&
in_array($chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['type'], $oid)
) {
$value = $chain['tbsCertificate'][$type]['rdnSequence'][$i][0]['value'][$field];
return $value;
$ca = '';
if(isset($chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'])) {
$ca = $chain['tbsCertificate']['extensions'][$j]['extnValue']['cA'];
}
}
}
}
}
return null;
}
}
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca == '1')) {
echo 'primary ca';
} else {
echo 'Root';
}
You are using =, which sets the value of $ca. You should be using === to check the value, instead.
Example:
if (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '')) {
echo 'end entity';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '0')) {
echo 'secondary ca';
}
elseif (lookup($chain, 'subject') != lookup($chain, 'issuer') && ($ca === '1')) {
echo 'primary ca';
} else {
echo 'Root';
}

How to go to 'else if' loop after 'if' condition is sucessfully entered

Is there any way to go to the else if body after the if condition is sucessfully entered?
Is there any way to go to the else if body after the if condition is sucessfully entered?
if ($axisX == $axisXOne) { /* Main IF Statement */
if($axisY =='0' && $axisYOne == '1') { $second = '2';}
else if($axisY =='0' && $axisYOne == '2') { $second = '1';}
else if($axisY =='1' && $axisYOne == '0') { $second = '2';}
else if($axisY =='1' && $axisYOne == '2') { $second = '0';}
else if($axisY =='2' && $axisYOne == '0') { $second = '1';}
else if($axisY =='2' && $axisYOne == '1') { $second = '0';}
if (($_POST['button'.$axisX.$second] == null) && ($curVal != $axisX.$second)){ /* Inner IF Statement */
echo $axisX.$second;
}
}
else if ($axisY == $axisYOne) { /* Main ELSE IF statement */
if($axisX =='0' && $axisXOne == '1') { $second = '2';}
else if($axisX =='0' && $axisXOne == '2') { $second = '1';}
else if($axisX =='1' && $axisXOne == '0') { $second = '2';}
else if($axisX =='1' && $axisXOne == '2') { $second = '0';}
else if($axisX =='2' && $axisXOne == '0') { $second = '1';}
else if($axisX =='2' && $axisXOne == '1') { $second = '0';}
if($_POST['button'.$second.$axisY] == null) {/* Inner IF Statement */
echo $second.$axisY;
}
}
else if ($xAxis == $yAxis && $xAxisOne == $yAxisOne) { /* other Main ELSE IF Statement*/
if($comVal[0] == '00' && $comVal[1] == '11') { $diagon = '22';}
else if($comVal[0] == '00' && $comVal[1] == '22') { $diagon = '11';}
else if($comVal[0] == '11' && $comVal[1] == '00') { $diagon = '22';}
else if($comVal[0] == '11' && $comVal[1] == '22') { $diagon = '00';}
else if($comVal[0] == '22' && $comVal[1] == '00') { $diagon = '11';}
else if($comVal[0] == '22' && $comVal[1] == '11') { $diagon = '00';}
if($_POST['button'.$diagon] == null) {
echo $diagon;
}
}
If Main IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main IF Statement evaluates true and Inner IF Statement evalautes true then stop loop . If Main IF Statement evaluates false, directly go to Main ELSE IF Statement
If Main ELSE IF Statement evaluates true and Inner IF Statement evalautes false then go to Main ELSE IF ladder. If Main ElSE IF Statement evaluates true and Inner IF Statement evalautes true ,then stop loop .If Main ELSE IF Statement evaluates false, directly go to other Main ELSE IF Statements
Try this:
$success = true;
if($a == $Xaxis && $b == $yaxis) {
$zAxis = $Xaxis + $yaxis;
$success = $Xaxis != $zAxis;
}
if( ($b == $Xaxis && $a == $yaxis) || !$success) {
// do stuff here
}
Or something along those lines.

Categories