I am very new in php. I am having a problem in condition checking. I have the condition chain like this
if(condition1){
if (condition2){
Statement1;
}else{
Statement2;
}
else{
if (same condition2){
Statement3;
}else{
Statement4;
}
Now when I am writing this like I am having errors -
if (condition1) "AND". (condition2)
Statement1;
if (condition1) "AND". (!condition2)
Statement2;
if (!condition1) "AND". (condition2)
Statement3;
if (!condition1) "AND". (!condition2)
Statement4;
Can anyone enlighten me where I am wrong?
Here is the real code I am writing -
if (!isset($store['stationfilter'])) "AND". ($fromdate_display == $todate_display)
$storeSql_current = sprintf($sql_current, ''); //error 1
if (!isset($store['stationfilter'])) "AND". (!$fromdate_display == $todate_display) //error2
$storeSql_otherThancurrent = sprintf($sql_otherThancurrent, ''); //error3
if (isset($store['stationfilter'])) "AND". ($fromdate_display == $todate_display) //error4
$storeSql_current = sprintf($sql_current, "AND" . $store['stationfilter']); //error5
if (isset($store['stationfilter'])) "AND". (!$fromdate_display == $todate_display) //error6
$storeSql_otherThancurrent = sprintf($sql_otherThancurrent, "AND" . $store['stationfilter']); //error7
Please tell me where I am wrong.
Thanks,
Neel
You should write like this:
if ($condition1 && $condition2) ....
if ($cond1 && $cond2) {
# stmt1
} else if ($cond1) {
# stmt2
} else if ($cond2) {
# stmt3
} else {
# stmt4
}
I've amended the above with your actual conditions, below. I've also taken a guess at what your statements are supposed to look like...
if ( isset($store['stationfilter']) && $fromdate_display === $todate_display ) {
$storeSql_current = sprintf('%s AND %s', $sql_current, $store['stationfilter']);
} else if ( isset($store['stationfilter']) ) {
$storeSql_otherThancurrent = sprintf('%s AND %s', $sql_otherThancurrent, $store['stationfilter']);
} else if ( $fromdate_display === $todate_display ) {
$storeSql_current = (string) $sql_current;
} else {
$storeSql_otherThancurrent = (string) $sql_otherThancurrent;
}
If what you're doing is to build a sql statement, then this approach may be dangerous because of the potential for SQL Injection. If $store['stationfilter'], $sql_current or $sql_otherThancurrent (the data) are coming from an untrusted source (all sources are untrusted unless the source is code you have written) then you should, at minimum, be using the database adapter to quote the data or, better, using parameterised queries.
Related
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
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.
I know there is a shorthand for IF/ELSE STATEMENT in PHP such as
($user['permissions'] == 'admin' ? true : false);
But is there a shorthand for ELSE IF statement besides switch?
What you could do
You can keep chaining ternary operators together, e.g.:
$x = $condition1 ? true : ($condition2 ? true : false);
It looks nice now, but once your conditions grow bigger, it quickly becomes unreadable. Note that parentheses are bare essentials for these kind of expressions.
What you should do
Once you add more conditions, prefer to use the proper branching syntax; always assume the person who later has to take over your code is a psychopath who knows where you live:
$canAccess = false;
if ($user['permissions'] == 'admin') {
$canAccess = true;
} elseif ($user['permissions'] == 'whatever') {
$canAccess = true;
}
Yes, you could use an or in the first statement too.
Or, a switch:
switch ($user['permissions']) {
case 'admin':
case 'whatever':
$canAccess = true;
break;
default:
$canAccess = false;
}
I’d rather just use elseif() {} anyway
$somevalue == 'foo' ? 'is foo' : ($somevalue == 'bar' ? 'is bar' : 'is neither');
I am just wondering if there is better way to solve my situatuion:
I have 6 independent variables to check. But if any of conditions is true it shouldnt check other. Normally I would write:
if (cond1 ) {
statement
} else {
if ( cond2 ) {
statement
} else {
if (cond3) {
statement
} else {
...
}
}
}
Surely you would admit it doesnt look good or it is not easy to read although it works. Do you know any other ways to write such if statement maybe using other notation or functions (switch? while?)
Yes, you can do
if (cond1 ) {
statement
} elseif ( cond2 ) {
statement
} elseif ( cond3 ) {
statement
}
See documentation
A more stylish way:
if(cond1):
statement1
elseif(cond2):
statement2
elseif(cond3):
statement3
elseif(cond4):
statement4
elseif(cond5):
statement5
elseif(cond6):
statement6
endif;
This is how you do it with a switch():
$a = 10;
$b = 100;
switch(true){
case ($a > $b):
echo 'a is bigger than b';break;
case ($b > $a):
echo 'b is bigger than a';break;
}
if (cond1 ) {
statement
} else {
if ( cond2 ) {
statement
} else {
if (cond3) {
statement
} else {
...
}
}
}
Change to:
if (Cond1){
}elseif (cond2){
}elseif (cond3){
}
I've got a small snippet of code below and I was curious what types of things you would change with regards to best practices/code maintainablity and so on.
function _setAccountStatus($Username, $AccountStatus)
{
if ($Username == '' || ($AccountStatus != 'Active' || $AccountStatus != 'Banned' || $AccountStatus != 'Suspended')) {
// TODO: throw error here.
}
$c1 = new Criteria();
$c1->add(UsersPeer::USERNAME,$Username);
$rs = UsersPeer::doSelect($c1);
if (count($rs) > 0) {
$UserRow = array_pop($rs);
$UserRow->setAccountStatus($AccountStatus);
try {
$UserRow->save();
} catch ( PropelException $e ) {
return false;
}
return true;
}
return false;
}
I would use the empty() instead of $Username == '' in your if statement. I haven't used propel before, but I would prefer to have this method be on my User object itself with the fetching and saving of the user object performed by a seperate object. Pseudo code would be something like this.
$user = userManager->getUser($username);
$user->setAccountStatus($accountStatus);
$userManager->saveUser($user);
An else clause before the last return false would be prefererred, just to make the code more readable.