How can i have and or together? [closed] - php

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){
}
}

Related

2 declaration of $_POST not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I need to use 2 $_POST. Here's the code but it's not working.
if($_POST['grade'] == 'G1') && ($_POST['district'] == 'D1')
Thanks in advance!
In my opinion, you should also check that $_POST is not empty before trying to access any key inside
<?php
error_reporting(E_ALL);
if (!empty($_POST)) {
if ($_POST['grade'] == 'G1' && $_POST['district'] == 'D1') {
#condition fulfilled.
} else {
print_r($_POST); #debug $_POST and see where you went wrong
exit;
}
} else {
die('POST GLOBAL IS EMPTY');
}
Try by this
if($_POST['grade'] == 'G1' && $_POST['district'] == 'D1'){
//Both Founded
}else{
//Not Founded
}

Logic for ifelse condition [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 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";
}
}

String count in $_POST [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a $_POST which contain following data
$_POST['survey_que1']
$_POST['survey_que2']
$_POST['survey_que3']
....
so on.
I have to get a count of occurrence of survey_que from $_POST.
Any Ideas please.
Restructure your data.
<input type="text" name="survey_que[]" />
This will result in $_POST['survey_que'] being an array of passed values, which you can easily use count() to get the total count.
try this
$i=0;
foreach ($_POST as $name => $val)
{
if(strpos($name,'survey_que') !== false && $name != "survey_que")
$i++;
}
echo "No of times survey_que occured in $_POST[] is ".$i;
EDIT
#shri in your code if survey_que2 is not set and survey_que3 is set then your code fails.
So try this workaround which covers all worst cases also
$i=0;
foreach ($_POST as $name => $val)
{
if(strpos($name,'survey_que') !== false && $name != "survey_que")
{
$key=explode("survey_que",$name);
if(sizeof($key) == 2 && is_numeric($key[1]) && $key[0] == "")
{
$i++;
}
}
}
echo "No of times survey_que occured in $_POST[] is ".$i;
Serialize $_POST and use "substr_count" for counting ocurrences
$how_many = substr_count(serialize($_POST), $search_string));
It's the easiest way I find but will count this text even if it's not a $_POST key value
This is working for me exactly i want.
$i = 1;
while (isset($_POST['survey_que' . $i])) {
$i++;
}
echo $i-1;
Thanks everyone for quick support.

easier way to validate $_GET id's [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
In my url i'm passing id's like this
localhost/?id=1,2,3,4,5,6,7,8,9
I was wondering if there is a better way / simpler way to validate each id without using a loop?
if (isset($_GET['id']) && !empty($_GET['id']))
{
$str = explode(',', $_GET['id']);
for($ids = 0; $ids < sizeof($str); $ids++)
{
if (!ctype_digit($str[$ids]))
{
echo 'error';
break;
}
}
}
You could just test the string with a simple regular expression, eg
if (preg_match('/^(\d+,)*\d+$/', $_GET['id']) == 0) {
throw new Exeption('error');
}

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.

Categories