PHP: confused about if else statement - same logic or wrong understanding? - php

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.

Related

Function always returning true no matter input

I trying to create a function that matches the first number in a string to a state. For example if the user inputs a number that starts with 3, and the state 'vic', then the form should not present any errors. However no matter what is entered the function always returns true. Any help would be appreciated.
$state = array('Please Select', 'VIC', 'NSW', 'QLD', 'NT', 'WA', 'SA', 'TAS', 'ACT'); //In the form this is a drop down menu
$selected_key = $_POST['state'];
$postcode = $_POST["postcode"]; //The full number entered by the user
$errMsg .= validatePS($postcode, $selected_key);
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$post = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$post = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$post = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
if ($errMsg !=""){
echo "<p>Please correct the following errors...</p>"; //Prints out errors
echo "<p>$errMsg</p>";
}
You have two "flag" variables - $post which you are updating and $valid which you rely on. If you reduce them to one variable, you should get the behavior you want:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if (($digit == 3) or ($digit == 8) && ($state == 'vic'))
{
$valid = true;
}
if (($digit == 1) or ($digit == 2) && ($state == 'nsw'))
{
$valid = true;
}
if (($digit == 4) or ($digit == 9) && ($state == 'qld'))
{
$valid = true;
}
if ($valid == false) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}
Note that you could clean up this code considerably by using logical operators instead of multiple if statements:
function validatePS($ps, $state) {
$errMsg ="";
$digit = $ps[0]; //Takes the first number from full postcode
$valid = false;
if ((($digit == 3) or ($digit == 8) && ($state == 'vic')) ||
(($digit == 1) or ($digit == 2) && ($state == 'nsw')) ||
(($digit == 4) or ($digit == 9) && ($state == 'qld'))) {
$errMsg .= "<p>Match the correct postcode to state</p>";
}
return $errMsg;
}

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

php IF OR format?

This works
If ($flag != 'u') { stuff.. }
This also works.
If ($id != 0) { stuff.. }
But these don't seem to work for me....
If ( ($flag != 'u') || ($id != 0) ) { stuff.. }
If ( ($flag != 'u') or ($id != 0) ) { stuff.. }
If ($flag != 'u') || ($id != 0) { stuff.. }
If ($flag != 'u') or ($id != 0) { stuff.. }
If ( $flag != 'u' || $id != 0 ) { stuff.. }
If ( $flag != 'u' or $id != 0 ) { stuff.. }
Any idea why? and what format should I use for this in PHP?
Your code actually works. I tried this
<?php
$flag= u;
$id= 0;
if ( ($flag != 'u') || ($id != 0) )
{
echo "Hi";
}
?>
The code simple says if $flag not equal to u or if $id not equals 0 then echo hi in page.
Is that your logic?
The problem might be in your logic I hope!

PHP if/else statement issue

I'm having some issues with this statement,
<?php
$cert_mess = $vehicle['make'];
if ($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0") {
$cert_mess = "DFWCertAutos";
}
elseif (!in_array($vehicle['make'], array('Cadillac','Honda') )) {
$cert_mess = "DFWCertAutos";
}
?>
<div style="font-size:10px; padding:10px; padding-top: 0px;">*This car is <?php
echo $cert_mess ?> certified.</div>
Any suggestions? currently it just displays $cert_mess as 'make' and ignores the if / else if statements.
A simpler code can be the following:
$cert_mess = $vehicle['make'];
if (($vehicle["make"] == "Honda" && $vehicle["Certified"] == "0")
|| ($vehicle["make"] == "Cadillac" && $vehicle["Certified"] == "0")
|| !in_array($vehicle['make'], array('Cadillac','Honda'))
) {
$cert_mess = "DFWCertAutos";
}
Simpler still:
$cert_mess = $vehicle['make'];
if (!in_array($vehicle['make'], array('Cadillac', 'Honda')) || $vehicle['certified'] == '0')
{
$cert_mess = 'DFWCertAutos';
}

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