Logic for ifelse condition [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I want to make a logic to check multiple conditions without using if else condition numbers of time.
I have 4 variables and i have to check whether it is empty or not.
if one of the variable is blank than want to do something.
I have function like this..
function searchResult($genre, $subject, $type, $grade){
// checking conditions here
}
please suggest me simple method.

You can use func_get_args, array_filter and func_num_args as well:
function searchResult($genre, $subject, $type, $grade){
if (count(array_filter(func_get_args())) < func_num_args()) {
echo 'One or many arguments are empty.. do something';
}
}
Pay attention to the array_filter function:
If no callback is supplied, all entries of array equal to FALSE (see converting to boolean) will be removed.

function searchResult($genre, $subject, $type, $grade){
if((!empty($genre)) && (!empty($subject)) && (!empty($type)) )
{
echo "not empty";
}
else
{
echo "empty";
}
}

Related

Using '&&' inside foreach? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am looking for smart solution, maybe you could help me out. Currently I am doing an own Authentication (Separate Class) system for my Webshop project. My problem is, that I need conditional statement inside foreach loop, to return the code (see below). Any suggestions?
My code currently look like this
public function regiAuth($email, $password, $firstname, $lastname)
{
$authContainer = [$email, $password, $firstname, $lastname];
foreach ($authContainer as $a) {
return !empty($_POST[$a]);
}
}
And I want to result this (With &&)
return !empty($_POST[$email]) && !empty($_POST[$password]) &&
!empty($_POST[$firstname]) && !empty($_POST[$lastname])
I believe you could do simply by do
foreach ($authContainer as $a) {
if (empty($_POST[$a])
return false;
}
return true;
instead of checking if all of them are full, you look if there is at least one empty.
it is a good practice to stop iteration if you find one element that is not as expected, imagine if you had an array of hundreads of assertions to do.
making an full if statement would look like this, here it checks if the $_POST are not empty. && means that both have to true or false
foreach ($authContainer as $a) {
if((!$_POST[$email]) && (!$_POST[$password])){
return false;
}
}
return true;

how to create "if" by values of an array elements in php [closed]

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 2 years ago.
Improve this question
i have an php array with Unknown number of elements (1 to 6 elements). the elements are boolean (false or true)
i want to write an if block that do some statements if all of array element are true and do some other statement if at least one of element value is false. can you help me?
Try something like this =)
$myarray = [true,true,true,true,true];
// no true elements
if (!in_array(true, $myarray)) {
echo 'no true elements';
}
// no false elements
if (!in_array(false, $myarray)) {
echo 'no false elements';
}
// at least one false element
if (in_array(false, $myarray)) {
echo 'at least one false element';
}

How can i have and or together? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
Im wondering if i can do and/or together. I'm trying to make a code that checks if both variables are wrong or if only one of them is wrong and the other one is right then I want to show a message like: "one of the "variables" are wrong.
Can anyone help me out here?
In addition to Tim Whites answer, if you don't want it nested, you could also use:
if($a == false AND $b == false) { echo "Both variables are false"; }
elseif($a == false OR $b == false) { echo "One variable is false"; }
if ($a==false OR $b==false) {
if($a==false AND $b==false) { echo "Both variables are false"; }
else { echo "On of the variables is false"; }
}
Something like that will do.
You could just use multiple IF statements, to check if 1 variable is wrong or both are wrong. Like:
if ($variable1==false && $variable2==false){
}
else{
if ($variable1==false || $variable2==false){
}
}

Error on line that doesnt exist [closed]

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 8 years ago.
Improve this question
I'm trying to define a link based of a $_GET variable, but it's saying there's an error on a line that doesn't exist...
<?php
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
?>
<?php
if ($ref != "") {
$link = "http://site.com/page.php?ref=$ref";
} else {
$link = "http://site.com/page.php";
}
?>
Anyone see what's up? I was pretty sure it was fine.
I've tried it multiple different ways, with isset etc... same result.
You are missing a closing }:
if(isset($_GET['ref'])){
if(!empty($_GET['ref']))
{
$ref = $_GET['ref'];
}
}
By the way, this code is quite redundant. empty() will also check whether the variable is set, so you don't need isset().
You can also use the ternary operator, which is for cases like this:
$ref = empty($_GET['ref']) ? null : $_GET['ref'];
And later check with:
if (!is_null($ref)) {
//whatever
}
Otherwise, in your code, when execution reaches if ($ref != "") {, the variable $ref might not even exist - this will throw an E_NOTICE, which you might not even see, depending on your settings.

Find a value in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What I am doing wrong with the following code? I want to compare if the element $my_id is present within the array $arr. If it is present return TRUE else return FALSE.
for($i=0;$i<$cnt;$i++)
{
if($arr[$i] == $my_id)
{
return TRUE;
}
else
{
return FALSE;
}
}
You could replace that with...
return in_array($my_id, $arr);
...assuming you don't really want to return FALSE if the first element does not match.
If that is actually what you wanted, you could use...
return $arr[0] == $my_id;
If you want to leave your code mostly intact, just move the return FALSE to outside of the loop body.
The issue you are having is that you aren't looping entirely through the array. You are returning true/false after the first item in the array, irrespective of subsequent array entries after [0]
Well, I believe you should remove the else statement, unless you always just have one element in the array. I mean -- from the example you're showing, you're exiting the loop with this. I doubt this is what you want.
If you just need to know, if a value is within a given array
in_array($value, $array);
Maybe you want get the index of (the first occurence of) the value too
$index = array_search($value, $array);
if ($index === false) {
// Not in array
} else {
echo $array[$index];
}
The error in Your code is to return false on $arr[$i] != $my_id. The algorithm should look something like this:
for($i=0;$i<$cnt;$i++)
{
if($arr[$i] == $my_id)
{
return TRUE;
}
}
return FALSE;
P.S. This isn't the best solution for this problem in PHP language. You should use one from alex.

Categories