How do I add an if / else? - php

I am using the following code to pass a variable. if variable = a, do nothing.
I then want to check if variable = a, do nothing, if b, do nothing, else do something
<?
if($_GET['pageid'] == 'a'){
} else {
include('header_image.php');
}
?>
Above is the code I have working correctly for one vartiable.
How do I add an if / else?

if($_GET['pageid'] != 'a' && $_GET['pageid'] != 'b'){
//do smth
}

This is a comment - i want the formatting...
To do what you want:
if ($_GET['pageid'] == 'a') {
// do nothing for now
}
elseif ($_GET['pageid'] == 'b') {
// do some more nothing...
}
else { // we do something...
include('header_image.php');
}
You could combine the 'do nothing' tests as:
if ( $_GET['pageid'] == 'a'
|| $_GET['pageid'] == 'b') {
// do nothing for now
}
else { // we do something...
include('header_image.php');
}
I agree it reads better than the 'not equal and' tests. However, that is what 'programmers' use so it is worthwhile getting used to it.

Related

If statement inside another one is false, return to the else of original if statement

Is there any way in PHP to return at else of first statement, if the second statement which is inside of first, is false
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
return to the else of $first statement
}
//other code which is not necessary to mention here!
}
else{
//do smth else
}
Yes, there are multiple ways. For starters, just combine both the statements and give another condition:
if ($first == true && $second == true) {
// do smth
} elseif ($first == true && $second == false) {
// else of$first statement
} else {
//do smth else
}
This can be used as a guidance to get an idea to start. But if you can get a real world example, there can be conditions grouped, tailored to your requirement.
While there is no native way to jump to outer elses from an inner else, but you can set a flag for later processing:
$do_else = false;
if($first == true) {
//other code which is not necessary to mention here!
if($second == true){
// do smth
}
else{
$do_else = true;
}
//other code which is not necessary to mention here!
}
else{
$do_else = true;
//do smth else
}
if($do_else){
//do smth else
}
If the answers above doesn t help you in the real situation, you can create a function for execute in 'else' statements to avoid code duplication

Advice in design about Control Structure else if, switch o a better way?

I made the script to do what is expected, so it work ok but there must be a more elegant way to achieve the same result. I know that using switch will make it look nicer but not sure if the result will be the same as the 'default:' behavior:
This is the section of the script i want to refactor:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
// Here i call a function that do a mysql query and
// return the user active tasks
$result = tasks($deadline,$user);
$row_task = mysql_fetch_array($result);
// HERE IS THE UGLY PART <<<<<----------------
// the array will return a list of tasks where this current
// users involved, in some cases it may show active tasks
// for other users as the same task may be divided between
// users, like i start the task and you continue it, so for
// the records, user 1 and 2 are involved in the same task.
// The elseif conditions are to extract the info related
// to the current $user so if no condition apply i need
// to change function to return only unnasigned tasks.
// so the i need the first section of the elseif with the
// same conditions of the second section, that is where i
// actually take actions, just to be able to change of
// change of function in case no condition apply and insert
// tasks that are unassigned.
if ($row_task['condition1'] == 1 && etc...) {
} else if ($row_task['condition2'] == 1 && etc...) {
} else if ($row_task['condition3'] == 1 && etc...) {
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
// in case no condition found i change function
// and overwrite the variables
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition3'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition4'] == 1 && etc...) {
} else {
echo 'nothing to insert</br>';
}
}
}
Basically i run the else if block twice just to be able to change of function in case nothing is found in the first loop and be able to allocate records unassigned.
I haven't changed the functionality of your code, but this is definitely a lot cleaner.
The main problem was that your logic for your if/else statements was confused. When you're writing:
if($a == 1){ } else if($b == 1){ } else if($c == 1){ }else{ //do something }
You're saying If a is 1 do nothing, if b is 1 do nothing, if c is 1 do nothing, but if all of those did nothing, do something when you can just say if a is not 1 and b is not 1 and c is not 1, do something.
I wasn't too sure on your second if statements, but generally it's not good to have an if else with no body within it. However, if the "insert into database" comment does the same thing, you can merge the 3 if statements that do the same code.
I hope i've cleared a few things up for you.
Here's what I ended up with:
foreach ($free_slots as $val) { // here i am looping through some time slots
$slot_out = $free_slots[$x][1];
$slot_in = $free_slots[$x][0];
$slot_hours = $slot_out - $slot_in;
// tasks
if ($slot_out != '00:00:00') {
$result = tasks($deadline, $user);
$row_task = mysql_fetch_array($result);
if (!($row_task['condition1'] == 1 || $row_task['condition2'] == 1 || $row_task['condition3'] == 1 || $row_task['condition4'] == 1)) {
$result = tasks($deadline,'');
$row_task = mysql_fetch_array($result);
}
if ($row_task['condition1'] == 1 && etc...) {
// insert into database
} else if ($row_task['condition2'] == 1) {
// insert into database
} else if ($row_task['condition3'] == 1) {
// insert into database
} else if ($row_task['condition4'] == 1) {
} else {
echo 'nothing to insert</br>';
}
}
}

Elseif not returning the proper value on PHP

When the user introduces a letter and clicks on submit the script shall return a name that starts with that letter, I wrote it using a Switch/Case structure, now I need to write it using if/elseif/else.
The problem is that no matter what letter I introduce on the Textbox I'll always get the return for A (Aberdefia - Anacleto)
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombradorIf = 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombradorIf = 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
The code for the letters C to Z is just like the one for A and B.
You need == for comparison, = is for assignment, and === is for identical or same type.
Also, you used $nombrador when assigning and $nombradorIf when comparing, resulting in an undefined variable.
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Thus, $nombrador == 'A' means $nombrador is equal to A.
And, $nombrador = 'A' means to assign A to $nombrador.
More information at http://php.net/manual/en/language.operators.comparison.php.
Hope this helps, thanks!
== is the conditional operator = is assigning operator
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Try this code
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
echo "Aberdefia - Anacleto";
} else if($nombrador == 'B') {
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}
Your code error near . you condition check variable name is $nombradorIf and also not declare variable but You assign the value variable name is $nombrador and you not use conditional operator == . so result not proper . You use above the code working fine
For checking conditional statement you have to use == (double equal sign) like below:
$nombrador=$_POST['nombrador'];
$nombrador=strtoupper($nombrador);
if ($nombrador == 'A') {
// ^^ Use like this
echo "Aberdefia - Anacleto";
} elseif ($nombrador == 'B') {
// ^^ Use like this
echo "Brígida - Brígido";
} else {
echo "Is that even a letter?";
}

Do I need to specify not to use the second variable in my second else if statement? If so, how?

If I want to create an if statement with 2 variables:
if ($variable1 && $variable2) {
// Do something
}
And then add another if statement below with only the first variable, how would I do it? Do I only include the one variable like this:
else if ($variable1) {
// Do something
}
Or do I need to specify that the first variable is true, not the second? If so, is this correct?
if ($variable1 && !$variable2) {
// Do something
}
Go for:
if ($variable1 && $variable2) {
// Do something
}
else if ($variable1) {
// Do something
}
the reason is for example if you write like this :
the following is wrong approach
if ($variable1) {
// if u have two variables $variable1 and $variable2
// and you want to validate both but if the $variable1 contains
// nonzero value it will never go to the else part
}
else if ($variable1 && $variable2) {
// Do something
}
now basically
else if ($variable1) {
// Do something
}
and
else if ($variable1 && !$variable2) {
// Do something
}
are same.you can use any of them if you are not toooo much concerned about the performance.
else if ($variable1) {
// Do something
}
is enough. Since the first if-statement will fail, it will evaluate the else if as a new statement

If statement with multiple logical comparison

I need to check if $_POST['a'] is not empty AND is either '1' OR '2' so user cannot remove the a= or change the value from 1 or 2 to something else from the post path:
<?php
if(empty($_POST['a']) || !in_array($_POST['a'], array('1', '2'))) {
echo 'error1';
} else if ($_POST['a'] == '1') {
do something;
} else if ($_POST['a'] == '2') {
do something;
} else {
echo 'error2';
}
?>
Can anyone teach me how to do this in a correct way?
Many many thanks
You can use a switch instead:
switch ($_POST['a']):
case '':
// empty
echo 'error1';
break;
case '1':
// do something for 1
break;
case '2':
// do something for 2
break;
default:
// not empty but not 1 or 2
echo 'error2';
endswitch;
if (!empty($_POST['a']) && $_POST['a'] == '1') { //Not empty AND is 1
do something;
} else if (!empty($_POST['a']) && $_POST['a'] == '2') { //Not Empty AND is 2
do something;
} else {
echo 'error';
}
The first two will catch all "good" values, everything else will get the error. And no need for the top if in this case.
UPDATE: You have a syntax error. Missing a ) at the end of the first if.
Two easy ways of doing this:
// first condition should be by itself as it's a terminal error
if(empty($_POST['a']) or !in_array($_POST['a'], array('1', '2'))) {
echo 'error1';
die; // or redirect here or just enfore a default on $_POST['a'] = 1; // let's say
}
// Second can be like this or embraced in the else of the first one (se ex.2)
if ($_POST['a'] == '1') {
// do something;
} else if ($_POST['a'] == '2') {
// do something;
}
or
// first condition should be by itself as it's a terminal error
if(empty($_POST['a']) or !in_array($_POST['a'], array('1', '2'))) {
echo 'error1';
// or redirect here or just enfore a default on $_POST['a'] = 1; // let's say
}else{ // Second is in the else here :)
if ($_POST['a'] == '1') {
// do something;
} else if ($_POST['a'] == '2') {
// do something;
}
}
Your last else will not be reached as it will always end in the first if where you handle both emptiness and illegal value.

Categories