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 3 years ago.
Improve this question
I've got an if statement like
if ( $id == '2781' || $id == '1947' || $id == '516' || $id == '3200' || $id == '237' || $id == '3207' || $id == '3205' || $id == '2647' || $id == '516' || $id = '4571' ) {
// Do something
}
But the if statement appears to be failing because of the number of conditions (as in, it always does something, not just on specified IDs). I can remove a few of them and it works as expected. How would I fix this?
Try this instead, in_array is perfect for that kind of situation:
if (in_array((int)$id, array(2781, 1947, 516, 3200, 237, 3207, 3205, 2647, 516, 4571))) {
// Do something
}
Also, you had a missing '=' in your code.
You have a typo:
... || $id = '4571' ) ...
is assigning value of '4571' to $id. You have to use the comparison operator:
... || $id == '4571' ) ...
That's why I like to use in_array:
$availableIds = ['2781', '1947', '516', '3200', '237', '3207', '3205', '2647', '516', '4571'];
if(in_array($id, $availableIds)) {
...
}
Related
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
}
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 8 years ago.
Improve this question
I'm fairly new to PHP so I was wondering whether or not you could have to 'commands' in an if statement, like:
$value = 1;
$values = 2;
if ($value == '1' & $values == '2') {
do something
} else {
do something else
}
Would that work? I probably have the syntax wrong however you get the gist. Haha
Thanks for the help.
if you are new to use conditional operators read first before implement.
http://php.net/manual/en/language.operators.comparison.php
$value = 1;
$values = 2;
if ($value == '1' && $values == '2') {
// do something
} else {
// do something else
}
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 8 years ago.
Improve this question
I have this PHP code and trying to get the value of the variable language from the url parameters when specified link is clicked.
How do I do this? The code below only gets the value of Java in the first statement and not the values in the elseif statements.
if (isset($_GET['language'])) {
if ($_GET['language'] = 'Java') {
$q = 'title:(Java+Developer)';
}
elseif ($_GET['language'] = 'PHP') {
$q = 'title:(PHP+Developer)';
}
elseif ($_GET['language'] = 'JavaScript') {
$q = 'title:(JavaScript+Developer)';
}
}
Links:
<li>Java</li>
<li>PHP</li>
<li>JavaScript</li>
There's a difference between = and ==. One is for assigning, the other for comparing.
In your first if() statement, you're assigning the value Java to $_GET['language'], which will evaluate to Java. This will then be true.
Change the single = in the comparisons to ==, and you should be all set.
You are using a single equals sign. This is an assignment (as you do when defining a variable).
You need to use double equals signs ==. This is how you test equality.
$_GET['language'] == SOME_STRING
In addition, I would recommend using a switch statement instead of multiple if statements:
if ( isset( $_GET[ 'language' ] ) ) {
switch( $_GET[ 'language' ] ){
case "Java":
$q = 'title:(Java+Developer)';
break;
case "PHP":
$q = 'title:(PHP+Developer)';
break;
case "JavaScript":
$q = 'title:(JavaScript+Developer)';
break;
}
}
Using a switch statement will make this code much easier to maintain and also to add extra conditions (other languages).
Check the equal sign! Replace "=" with "=="
try with this code
if (isset($_GET['language'])) {
if ($_GET['language'] == 'Java') {
$q = 'title:(Java+Developer)';
}
elseif ($_GET['language'] == 'PHP') {
$q = 'title:(PHP+Developer)';
}
elseif ($_GET['language'] == 'JavaScript') {
$q = 'title:(JavaScript+Developer)';
}
}
You are assigning value Java to $_GET["language"] which always returns true. You must compare the value of $_GET["language"] against a string. Compare using == or === operator.
if ($_GET["language"] === "Java") {
/* here be dragons */
}
It is also good habit to use Yoda Conditions to catch this kind of errors.
if ("Java" === $_GET["language"]) {
/* here be dragons */
}
You are using "=" here which is for assigning the variable some value.
Have a look at the use of these:
"=" is used for assigning a value to some variable.
"==" is used for comparison (regardless of the data type). For example if
$var =1 and we evaluate a condition
if($var == TRUE )
The result will be bool True because TRUE is 1 and FALSE is 0 always.
3.
"===" comparison based on datatype also. The above condition will evaluate to Bool False because the data types are different.
You should use == instead of =.
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm relatively new to PHP. My question is, how do I use OR logic when I have parentheses? I have the following line of code:
if (get_option('nl_heading_font') === "Alegreya Sans") {
//do something
}
Id like to add:
if (get_option('nl_body_font') === "Alegreya Sans") {
//do something
}
Can I do this:
if (get_option('nl_body_font' || 'nl_heading_font') === "Alegreya Sans") {
//do something
}
Or
if (get_option('nl_body_font') === "Alegreya Sans") || (get_option('nl_heading_font') === "Alegreya Sans") {
//do something
}
Based on your question, the best option is this. But you need to add extra parenthesis for the OR/|| clause like so:
if ((get_option('nl_body_font') === "Alegreya Sans") || (get_option('nl_heading_font') === "Alegreya Sans")) {
//do something
}
The construct of if() requires it’s own parentheses. So if you want to nest more conditions, you need to add more parentheses. But looking at your code you can probably remove the extra parentheses around each clause & it would be fine:
if (get_option('nl_body_font') === "Alegreya Sans" || get_option('nl_heading_font') === "Alegreya Sans") {
//do something
}
But will all that said, looking at your logic better way to handle this is to use an array. So your code would be like this:
$nl_body_font_array = array('nl_body_font', 'nl_heading_font');
foreach ($nl_body_font_array as $nl_body_font_value) {
if (get_option($nl_body_font_value) === "Alegreya Sans")) {
//do something
}
}
This way, you simple create an array with as many values as you want and just push them through the if logic via a loop. Easier to read & debug as your codebase grows.
This is the php or format
if((/// your check condition) || (/// your check condition) || (/// your check condition) ){
/// Your Working code Here////////
}