big IF function not working in PHP? - php

Dears,
I am surprised why the PHP 'IF ELSE' function is not working properly. I guess it's a bit long but I made Algorithm for it and logically it would work perfectly. Can anyone have any clue why the function not working properly . please ..
<?php
//entry Marks
$tc=80;$tf=33;$pc=null;$pf=50;
if($tc!=NULL && $tf!=NULL && $pc!=NULL && $pf!=NULL){
echo $tc." ".$tf." ".$pc." ".$pf;
}else{
if($tc!=NULL){
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
if($pf!=NULL){
echo "tf.pf";
}else{
echo "tf";
}
}
}
}else{
if($pc!==NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}else{ //2nd part
if($tf!=NULL && $pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($tf!=NULL){
if($pc!=NULL && $pf!=NULL){
echo "tf.pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "tf.pc.pf";
}else{
echo "tf.pc";
}
}else{
echo "tf";
}
}
}else{
if($pc!=NULL && $pf!=NULL){
echo "pc.pf";
}else{
if($pc!=NULL){
if($pf!=NULL){
echo "pc.pf";
}else{
echo "pc";
}
}else{
if($pf!=NULL){
echo "pf";
}else{
echo "null";
}
}
}
}
}
}
}
?>

Your code is really difficult to follow, this code sample below shows a simplified version which appends to a string the values or text depending on if the variable is NULL or not. You can change what the if-else statements add to the result string easily. At the end I remove the "." at the end of the final string.
$result = "";
if($tc == NULL){
$result .= "tc.";
}else{
$result .= $tc.".";
}
if($tf == NULL){
$result .= "tf.";
}else{
$result .= $tf.".";
}
if($pc == NULL){
$result .= "pc.";
}else{
$result .= $pc.".";
}
if($pf == NULL){
$result .= "pf.";
}else{
$result .= $pf.".";
}
$output = rtrim($result, '.');
echo $output;
I will update my answer when its a bit clearer what output you are after

As noted in the other answer, your code is much longer than needed. This length makes it hard to follow. You could use a foreach loop to go through all your columns (which will make adding columns much easier).
Assuming I understand what you are looking for, you could use something like:
$tc=80;$tf=33;$pc=null;$pf=50;
$columns = array('tc'=>$tc,'tf'=>$tf,'pc'=>$pc,'pf'=>$pf);
$i = 0;
$output = "";
foreach ($columns as $key=> $column) {
if (!$column == null) {
$output .= $key.".";
}
}
// strip last period:
$output = rtrim($output, '.');
echo $output;
Link to php sandbox for demo here.

I do not have the answer of why your large if-statement do not work. But I do have a suggestion of making a PHP function to handle your specific need:
function check($tc=null,$tf=null,$pc=null,$pf=null){
$r = array();
if($tc != null){ $r[] = 'tc'; }
if($tf != null){ $r[] = 'tf'; }
if($pc != null){ $r[] = 'pc'; }
if($pf != null){ $r[] = 'pf'; }
if(empty($r)){
return 'null';
} else {
return implode('.', $r);// MAKE STRING OF ARRAY
}
}
And call the function like this:
$tc=80;$tf=33;$pc=null;$pf=50;
echo check($tc, $tf, $pc, $pf);
Try it out: https://eval.in/735202

Related

PHP shorthand if echo else assign variable

I want to write this in one line if possible:
if ($some_var === true) {
$return .= $input;
} else {
echo $input;
}
Obviously I don't want this:
if ($some_var === true) { $return .= $input; } else { echo $input; }
but a shorter version of it.
I looked at other answers but I only find the echo (expression) ? true : false; statements. I don't want to echo on the true, only on the false.
$some_var = true;
$input = 'abc';
$return = '123';
echo ($some_var === true) ? ($return .= $input) : ($input);

PHP ldap_get_entries returns null but only on one system not on another

I'm trying to connect to Active directory and validate my user which works and the I retrieve a certain field which returns and employeecode that stored in AD for our payroll/ESS application, this code has been working on multiple different clients but suddenly at one client the code runs through until it get to the ldap_get_entries, the ldap_search ran successfully but nothing is getting returned in get_entries
If checked some similar problems where people changed sAMAccount to uid or email in the filter but that hasn't helped me solve this, does anyone maybe have an idea whatI missed that would make this code fail on one system but work fine on others
the magic happens in the second function (RetrieveADEntry), the first(Authenticate) is just to show my connection
public function authenticate()
{
error_reporting(0);
//10.0.4.22
$this->ldapConnection = ldap_connect($this->mHost, $this->mPort);
if(isset($this->ldapConnection))
{
if(trim($this->mUsername) === "")
{
$this->mErrorCode = ERR_USERNAME_REQUIRED;
$this->mConnected = false;
return false;
}
else if(trim($this->mPassword) === "")
{
$this->mErrorCode = ERR_PASSWORD_REQUIRED;
$this->mConnected = false;
return false;
}
echo "pre bind";
ldap_set_option($this->ldapConnection, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldap, LDAP_OPT_REFERRALS, 0);
if ($this->mGroup == null)
{
$ldaprdn = $this->mPrdn . "\\" . $this->mUsername;
}
else
{
$ldaprdn = 'cn='.$this->mGroup.$this->mPrdn . "\\" . $this->mUsername;
}
$mConnected = ldap_bind($this->ldapConnection, $ldaprdn, $this->mPassword);
if ($mConnected)
{
$this->retrieveADEntry();
echo "Binded";
if ($this->mErrorCode == UNAUTHORIZED)
{
$this->mErrorCode = UNAUTHORIZED;
$this->mConnected = false;
}
else if ($this->mErrorCode == ERR_LOGIN_FAILED)
{
$this->mErrorCode = ERR_LOGIN_FAILED;
$this->mConnected = false;
}
else
{
$this->mErrorCode = SUCCESSFUL;
$this->mConnected = true;
if ($data->{"rlogcompanycode"} != ''){
$this->setCompanyCode(trim((string)$data->{"rlogcompanycode"}));
}
}
}
else
{
echo "Not binded";
$this->mErrorCode = ERR_LOGIN_FAILED;
$this->mConnected = false;
}
return $this->mConnected;
}
else{
$this->mErrorCode = ERR_CONNECTION_FAILED;
$this->mConnected = false;
return false;
}
error_reporting(E_ALL);
}
private function retrieveADEntry()
{
//$ldap_base_dn = 'DC='.$this->mDC.',DC='.$this->mDomain;
$ldap_base_dn = "OU=group,DC=domain,DC=co,DC=za";
$filter = "";
$attr = array(
$this->mField,
"sAMAccountName",
);
$filter .="(sAMAccountName=$this->mUsername)";
$search_results = ldap_search($this->ldapConnection,$ldap_base_dn, $filter);
//For each account returned by the search
if (FALSE !== $search_results ){
$entries = ldap_get_entries($this->ldapConnection, $search_results);
$values = ldap_get_values($this->ldapConnection,$search_results, $attr);
$access = 0;
//For each account returned by the search
echo "succesfull query";
echo $entries['count'];
echo $this->mUsername;
var_dump($values);
for ($x=0; $x<$entries['count']; $x++)
{
echo "in loop";
var_dump($entries);
if (strpos( $entries[$x]['memberof'][0], $this->mGroup)) //Check if member is part of specified group
{
echo "GroupCheck1";
$access = 1;
$group = $this->mGroup;
}
if ($this->mGroup == null)
{
echo "GroupCheck2";
$access = 1;
}
echo "PostGroupChecks";
echo $access;
if ($access != 0)
{
echo "access";
echo $this->mField;
echo $entries[$x]['sAMAccountName'][0];
if (!empty($entries[$x][$this->mField][0]))
{
$this->setEmpkey($entries[$x][$this->mField][0]);
echo $entries[$x][$this->mField][0];
}
echo "return succesfull";
$this->mConnected = true;
$this->mErrorCode = SUCCESSFUL;
}
else
{
echo "No Access";
$this->mConnected = false;
$this->mErrorCode = UNAUTHORIZED;
} //END for loop
}
//END FALSE !== $result
ldap_unbind($ldap_connection); // Clean up after ourselves.
}
else
{
$this->mConnected = false;
$this->mErrorCode = ERR_LOGIN_FAILED;
}
return $this->ldapEntry;
}
PS. I also wrote a C# script for our desktop app which works perfectly fine on this system just like on all the rest and doesn't give this issue

PHP empty variable inside condition

I have a condition in which it is using a variable to pull either a number through 0-17, the string "MAKEUP", or the variable will be empty. I would like it to output the text "WIN" if the variable is greater than the number 8 and "LOSS" if the variable is less than the number 9. I would also like it to out "MAKEUP" if the variable consist of the string MAKEUP, and to display nothing if the variable is empty. Seems pretty simple to me, but I'm having issues particularly with the empty part. Can anyone let me know what I am doing wrong here? Code below
<?php
$t1w8 = '';
$result = $t1w8;
if ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result < 9 && !empty($result)) {
echo 'LOSS';
} elseif ($result == 'MAKEUP') {
echo '-';
} else {
echo 'yooo';
}
?>
Make some changes in your conditions like this
<?php
//$result = "MAKEUP";
$result = 0;
if ($result === 'MAKEUP') {
echo '-';
}else if (is_numeric($result) && $result < 9 ) {
echo 'LOSS';
}else if (is_numeric($result) && $result >= 9 ) {
echo 'WON';
} else{
echo 'yooo';
}
?>
Live demo : https://eval.in/897120
try with this code
<?php
//$result = "MAKEUP";
$result = "";
//$result = "9";
//$result = "-";
if ($result == 'MAKEUP' && !empty($result) ) {
echo '-';
} elseif ($result > 8 && !empty($result)) {
echo 'WON';
} elseif ($result <= 8 && !empty($result)) {
echo 'LOSS';
} else {
echo 'yooo';
}
?>
for demo :demo code here
You have explained that your number range is from 0-17.
You have also explained that you could get the word MAKEUP.
Based upon those constraints we could use something like this
$output = "";
// Do we have something?
if(strlen($result) > 0) {
if (strtolower($result) == "makeup") {
$output = "MAKEUP";
}
// assumes a single digit string
else if ($result < 9) {
$output = "LOSS";
} else if ($result <= 17) {
$output = "WIN";
}
}
echo $output;

Adjust the comma using only if-else

<?php
$one = "A";
$two = "f";
$three = "";
$four = "d";
$output = "";
$output = $one.",".$two.",".$three.",".$four;
if ($four == "") {
echo $output;
}
elseif ($three == "") {
echo $output;
}
elseif ($two == "") {
echo $output;
}
elseif ($one == "") {
echo $output;
}
?>
Hi, the output of the above program gives me as A,f,,d now i don't want the empty space between two values if i omit any one of them,can any one please tell me how to do that.
Please note that i need to use only if-else statements and no loops can be used.
Thank you.
Can you use arrays? You could do something like
$output = implode(',', array_filter(array($one, $two, $three, $four)));
<?php
$one = "A";
$two = "f";
$three = "";
$four = "d";
$output = "";
if($output && $one){
$output .= ",".$one;
}else{
$output .= $one;
}
if($output && $two){
$output .= ",".$two;
}else{
$output .= $two;
}
if($output && $three){
$output .= ",".$three;
}else{
$output .= $three;
}
if($output && $four){
$output .= ",".$four;
}else{
$output .= $four;
}
echo $output;
?>
check here : https://eval.in/540856

mt_rand() returns unexpected output

I am working with my ad system of my site and having trouble with this.
<?php
function bdads($size, $company) {
if($company == 'nufa') {
if ($size == '300'){
echo 'n300';
}
if ($size == '160'){
echo 'n160';
}
if ($size == '728'){
echo 'n728';
}
if ($size == '700'){
echo 'n700';
}
}
if($company == 'gnr') {
if ($size == '300'){
echo 'g300';
}
if ($size == '160'){
echo 'g160';
}
if ($size == '728'){
echo 'g728';
}
if ($size == '700'){
echo 'g700';
}
}
}
function bdad($size, $company){
$zsize = $size;
if($company == 'nufa'){
echo bdads($zsize, 'nufa');
}
if($company == 'gnr'){
echo bdads($zsize, 'gnr');
}
if($company == 'both'){
$RandomList = [ bdads($zsize, 'gnr'), bdads($zsize, 'nufa')];
echo $RandomList[mt_rand(0, count($RandomList) - 1)];
}
}
?>
Now, Everything seems fine.. as example,
<?php echo bdad(728, 'gnr'); ?>
returning g728 (as expected)
<?php echo bdad(300, 'nufa'); ?>
returning n300 (as expected)
But all trouble is in generating random content.
<?php echo bdad(300, 'both'); ?>
returning g300n300
I want it to choose either g300 or n300 randomly.
EDIT:
Changed $a to $RandomList, but still same result
I tried to simplify your functions a bit:
You have to return your values, otherwise your functions will return NULL by default. Also you can access a string like an array, so I used $company[0] to get the first letter of the company, which you then can concatenate with the size.
<?php
function bdads($size, $company) {
return $company[0] . $size;
}
function bdad($size, $company){
if($company == "both") {
$RandomList = [bdads($size, "gnr"), bdads($size, "nufa")];
return $RandomList[mt_rand(0, count($RandomList) - 1)];
} else {
return dads($size, $company);
}
}
echo bdad(300, "both");
?>
output:
n300 //Or g300

Categories