PHP: Can somebody simplify this IF statement for me? - php

if($object_type == 'regular') {
if($u_login == $object_user || $u_access >= 3 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
} else
if($object_type == 'comment') {
if($u_login == $object_user || $u_access >= 2 || $object_access >= 4) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
So the thing that if object is different type user need to have diff access level. How this statement can be simplified for don't having duplicates in it?
I generally forgot about groups in if's, thank you for reminding me it!
if($u_login == $object_user || $object_access >= 4 || ($object_type == 'regular' && $u_access >= 3) || ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}

Both conditions check for $u_login == $object_user and $object_access >= 4, with only the $object_type and $u_access differing. As such, you can bring these two checks up a level, and check against $object_type and $u_access >= 3 inside the outer condition.
As such, the statement can be re-written like this, shrinking one line of code:
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Although depending on your definition of 'simplify', you could also cut out the outer conditional entirely by making use of some brackets:
if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'regular' && $u_access >= 3)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
else if(($u_login == $object_user || $object_access >= 4) && ($object_type == 'comment' && $u_access >= 2)) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
However, it's worth nothing that both of your conditionals currently do the exact same thing, so the code could even be simplified to:
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
Hope this helps! :)

This probably isn't the best place to ask for help refactoring your code, but what the heck. Notice that you have two conditions that are exactly the same and being checked in both if conditions. Why not pull those up to the root level?
if($u_login == $object_user || $object_access >= 4) {
if($object_type == 'regular' && $u_access >= 3) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
if($object_type == 'comment' && $u_access >= 2) {
echo '<p> </p>';
echo '<p><span class="r_button">'.ex_lang('str_btn_delete').'</span></p>';
}
}
Notice that we don't need an else anywhere in here because the conditions provided are, by nature, mutually exclusive. This can make readability a bit simpler.

Related

Following code is not reaching the else statement

Disclaimer: I'm new to programming, this is a simple exercise I'm doing so go easy on me.
The following code is not reaching (printing) the else statement. Any insight is much appreciated.
$number = '1120011';
// Decimal check
if($number[0] == '+' && $number[1] == '0'){
echo "Error: Can't assign a plus sign on a leading zero!";
// Leading zero check
} elseif($number[0] == '+' || $number[0] == '-'){
echo "Number is decimal";
// Binary check
} elseif (preg_match('~^[01]+$~', $number)) {
echo "Number is binary";
// Code not executing here. For e.g. $number = 1120011
} else {
"Number is non binary";
}
Another question:
Why is the following 'if' not working properly (if I replace it in the code above). I guess it has something to do with bad usage of operators.
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
Thanks in advance! :)
For the second question:
if($number[0] == '+' || $number[0] == '-' && $number[1] == '0')
try
if(($number[0] == '+')||(($number[0] == '-')&&($number[1] == '0')))

How to use multiple && ORs in a PHP If statement?

How I adjust this in order to get the brackets to work?
if (mysql_num_rows($printPricealertsdata) < 5 && ($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR ($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])) {
echo "success";
}else{
echo "fail";
}
It would be easier if you told us what you want, and what you get with your code. But since you make me guess, I think you print "success" sometimes, even when the number of rows is 5 or more; and you don't want that. If that's the problem you need brackets around your OR-clause, because && binds closer. Check the manual for the precedence of different operators.
BTW you can use new lines and indentation to make your code more readable:
if ( mysql_num_rows($printPricealertsdata) < 5
&& (
($_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd'])
OR
($_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd'])
)) {
echo "success";
} else {
echo "fail";
}
Try this (without placing brackets around each condition):
if(mysql_num_rows($printPricealertsdata) < 5 && $_POST['pricetype'] == "pricebelow" && $_POST['price'] < $marketdata['priceusd']) OR $_POST['pricetype'] == "priceabove" && $_POST['price'] > $marketdata['priceusd']) {
echo "success";
}else{
echo "fail";
}

delete if/else statement in php

I am trying to learn PHP, and I am playing around with if loops, just to learn how to use them. I have made 8 variables containing a letter. After this I have made 8 if/else sentences that is writing these letters out.
<?php
$letter1 = 'N';
$letter2 = 'E';
$letter3 = 'K';
$letter4 = 'T';
$letter5 = 'A';
$letter6 = 'R';
$letter7 = 'I';
$letter8 = 'A';
if($letter1 == 'N') {
echo $letter1;
}
else {
echo 'FALSE';
}
if($letter1 == 'N' && $letter2 == 'E') {
echo $letter1.$letter2;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K') {
echo $letter1.$letter2.$letter3;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T') {
echo $letter1.$letter2.$letter3.$letter4;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A') {
echo $letter1.$letter2.$letter3.$letter4.$letter5;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R' && $letter7 == 'I') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6.$letter7;
}
else {
echo FALSE;
}
if($letter1 == 'N' && $letter2 == 'E' && $letter3 == 'K' && $letter4 == 'T' && $letter5 == 'A' && $letter6 == 'R' && $letter7 == 'I' && $letter8 == 'A') {
echo $letter1.$letter2.$letter3.$letter4.$letter5.$letter6.$letter7.$letter8;
}
else {
echo FALSE;
}
?>
So the output of this is:
NNENEKNEKTNEKTANEKTARNEKTARINEKTARIA
I have to questions I hope you can help me with.
1: If I want the output to be:
N
NE
NEK
NEKT etc etc. How do I make the space between then lines. Fx like html
2: can I do anything to ignore my if/else statements afterwords? Fx if I only wants to write out "Nektaria" and not N, NE, NEK, NEKT etc. I could of course just delete them, but it is just to make some small assignments for myself.
Best Regards
Mads
To answer your first question about spaces, then you would have to add those in your echo
example:
echo $letter1.$letter2. ' ';
For you second question, you could use switch and sort of make a menu with numbers. Example if 4 was pressed then print out whatever letters.
<?php
switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
default:
echo "i is not equal to 0, 1 or 2";
}
?>
http://php.net/manual/en/control-structures.switch.php
From this answer
PHP (typically) generates HTML output for a web-site.
When displaying HTML, the browser (typically) collapses all whitespace in text into a single space. Sometimes, between tags, it even collapses whitespace to nothing.
So you would just want to add a <br/> at the end of your echo statements like so:
echo $letter1.$letter2.$letter3.'<br/>'; //And so on for all the other echo statements
Now to answer the second part of your question, the if-else statements get executed sequentially. That is the whole reason why you see the output in that order. If you want to check for all the letters first (which outputs "NEKTARIA"), you'll just have to perform that check first and re-order your if-else statements. To ignore the if-else statements, you could use a simple $FLAG variable and set it to TRUE or FALSE depending on if it has performed the first check or not. So once you have performed the check you need, just do:
$FLAG = true;
And in all the if-else statements just check if $FLAG == false and only then proceed.
The switch statement as many have already pointed out, resolves these two issues directly and allows you to encapsulate multiple conditions and selectively check them.
switch (n) {
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
case label3:
code to be executed if n=label3;
break;
...
default:
code to be executed if n is different from all labels;
}
The code is pretty intuitive and self explanatory. Here is the link of the official W3 Schools tutorial on it which could be a great starting point for you. Hope this gets you started in the right direction.
1) For making your output like this: N NE NEK NEKT
Make this change in the echo statement :
echo $letter1." ";
echo $letter1$letter2.$letter3." ";
and so on for printing a space between two variables all you need to do is add this part after the variable you want to have a space: ." "
2) And for making your out put like this: Nektaria
don't echo out multiple variables in each and every if else statement. just one variable in each if-else loop.

add Number after the variable and echo it

I want to echo the html inside The variables $p1 $p2 $p3 etc
So when i click the button next it goes to next page
when i click button previous it goes 1 page back.
This is my working code:
if ($pageShow == 0) {
echo "<center>".$p1."</center>";
} elseif ($pageShow == 1) {
echo "<center>".$p1."</center>";
}elseif ($pageShow == 2) {
echo "<center>".$p2."</center>";
}elseif ($pageShow == 3) {
echo "<center>".$p3."</center>";
}elseif ($pageShow == 4) {
echo "<center>".$p4."</center>";
}elseif ($pageShow == 5) {
echo "<center>".$p5."</center>";
}elseif ($pageShow == 6) {
echo "<center>".$p6."</center>";
}elseif ($pageShow == 7) {
echo "<center>".$p7."</center>";
}elseif ($pageShow == 8) {
echo "<center>".$p8."</center>";
}elseif ($pageShow == 9) {
echo "<center>".$p9."</center>";
}
}
but i wanted to do it like this so it doesnt have that much lines of code:
echo '$p'.$_SESSION['hits'];
So when session is equal to 1 i tought the code would be echo $p1; and when Session is equal to 5 the code would do this echo $p5;
Use single quotes ' for echoing a variable name. eg
echo '$p';
Then do what you did before:
echo '$p'.$_SESSION['hits'];
If $_SESSION['hits'] is 5, then it will read $p5.
echo "\$p".$_SESSION['hits'];
this returns as $p5
http://www.php.net/manual/en/language.types.string.php#language.types.string.parsing
By using the ", PHP is parsing the string and echoing the value of $p. Instead use ' or escape the dollar sign. Like:
echo '$p' . $_SESSION['hits'];
or
echo "\$p" . $_SESSION['hits'];
If I am understating that you are about to refer a dynamic variable try this
$sessionHits = 'p'.$_SESSION['hits'];
$finalOp = $$sessionHits; // this will output if you have stored something in $p5 , as in this case $_SESSION['hits'] = 5

PHP && or if else statement not working correctly

So I have this survey that I am pulling results from and I have to basically make a bunch of if...else if... statements involving which feedback is submitted.
I have set it up to work correctly...except for one problem. I know it's probably something stupid, but for the life of me I can't figure it out.
Anyways here is the code.
if (($QID_A == 23) && ($row_result_2['Feedback'] == "")) {
echo "";
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] <= 3)) {
echo $row_custom['Content'];
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] == 4) || ($row_result_2['Feedback'] == 5) || ($row_result_2['Feedback'] == 6)) {
echo $row_custom['Content2'];
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] >= 7)) {
echo $row_custom['Content3'];
}
The problem I am getting is if $row_result_2['Feedback'] is == to 5 or 6. It just repeats the line over and over again, basically failing to evaluate. It works fine for 4, so I am thinking my problem is somewhere in how I set up the || statements?
Any help would be appreciated. Thank you.
While I'm at it would there be a simpler way to write this statement? So i don't have to copy it for each QID_A field 1-50?
UPDATE::
At this point I am at a loss as to why none of the below solutions are working.. I think this might be a deeper issue as the results are still repeating 24 times... Being that QID_A is 23 I think this might be related.. but I can't seem to find a connection... Thanks again for the help though guys I appreciate it.
Just for the hell of it I tried writing the statements out individually.... same result...
for example.
if (($QID_A == 22) && ($row_result_2['Feedback'] == "")) {
echo "";
}
else if (($QID_A == 22) && ($row_result_2['Feedback'] <= 3)) {
echo $row_custom['Content'];
}
else if (($QID_A == 22) && ($row_result_2['Feedback'] == 4)) {
echo $row_custom['Content2'];
}
else if (($QID_A == 22) && ($row_result_2['Feedback'] == 5)) {
echo $row_custom['Content2'];
}
else if (($QID_A == 22) && ($row_result_2['Feedback'] == 6)) {
echo $row_custom['Content2'];
}
else if (($QID_A == 22) && ($row_result_2['Feedback'] >= 7)) {
echo $row_custom['Content3'];
}
You need an extra pair of brackets to make it obvious what the precedence should be with your operators (and you don't need the brackets round each expression):
else if ($QID_A == 23 && ($row_result_2['Feedback'] == 4 || $row_result_2['Feedback'] == 5 || $row_result_2['Feedback'] == 6)) {
echo $row_custom['Content2'];
}
But you might be better off writing this logic use a case so it's clearer, i.e.
if ($QID_A == 23) {
switch( $row_result_2['Feedback'] ) {
case '':
echo '';
break;
case 0:
case 1:
case 2:
case 3:
echo $row_custom['Content'];
break;
case 4:
case 5:
case 6:
echo $row_custom['Content2'];
break;
default:
echo $row_custom['Content3'];
break;
}
}
You could use an if with a switch statment.
if ($QID_A == 23) {
$feedback = $row_result_2['Feedback'];
switch ($feedback) {
case "":
echo "";
break;
case 1:
case 2:
case 3:
echo $row_custom['Content'];
break;
case 4:
case 5:
case 6:
echo $row_custom['Content2'];
break;
default:
echo $row_custom['Content3'];
}
}
I realize that the switch can get a little kludgey if you end up with more options but makes things a little easier. You would also be able to break it into a function for the options 1 - 50 of the QID_A field.
http://www.php.net/manual/en/language.operators.precedence.php
I think you want:
else if (($QID_A == 23) && (($row_result_2['Feedback'] == 4) || ($row_result_2['Feedback'] == 5) || ($row_result_2['Feedback'] == 6))) {
As for your other question, if you need have 50 questions, create a map, something like:
array(qID => array(array(valuesOfFeedback), contentNumber)
if( in_array($feedback, $map[$QID_A][0]) ) echo $map[$QID_A][1];
if you are saying that you don't get the same output for 5 and 6 as you do for 4 I don't know what's the problem (maybe you should double check the value)
But for the second part of the question, the one about another way to write this same thing I have this option:
if (23 == $QID_A)
{
switch ($row_result_2['Feedback'])
{
case '':
echo '';
break;
case 1:
case 2:
case 3:
echo $row_custom['Content'];
break;
case 4:
case 5:
case 6:
echo $row_custom['Content2'];
break;
default:
echo $row_custom['Content3'];
break;
}
}
I don't know what's the role of $QID_A but just for this part it seems to be the same
Try this code, which is a little more efficient, more legible and (hopefully) will fix the problem:
if ($QID_A == 23) {
switch (TRUE) {
case $row_result_2['Feedback'] <= 3:
echo $row_custom['Content'];
break;
case $row_result_2['Feedback'] <= 6:
echo $row_custom['Content2'];
break;
case $row_result_2['Feedback'] <= 7:
echo $row_custom['Content3'];
break;
}
}
See if this does it.
<?php
if (($QID_A == 23) && ($row_result_2['Feedback'] == "")) {
echo "";
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] <= 3)) {
echo $row_custom['Content'];
}
else if (
($QID_A == 23 && $row_result_2['Feedback'] == 4) ||
($QID_A == 23 && $row_result_2['Feedback'] == 5 )||
($QID_A == 23 && $row_result_2['Feedback'] == 6)
) {
echo $row_custom['Content2'];
}
else if (($QID_A == 23) && ($row_result_2['Feedback'] >= 7)) {
echo $row_custom['Content3'];
}
?>
Instead else if (($QID_A == 23) && ($row_result_2['Feedback'] == 4) || ($row_result_2['Feedback'] == 5) || ($row_result_2['Feedback'] == 6)) write else if (($QID_A == 23) && (($row_result_2['Feedback'] == 4) || ($row_result_2['Feedback'] == 5) || ($row_result_2['Feedback'] == 6)))
Why? If && had the same priority as || your code, your code would work.

Categories