Page 1 of 1

if else or if statement

Posted: Tue Aug 07, 2007 12:55 pm
by shivam0101
which is correct, both works fine.

Code: Select all

$cond=$_POST['cond'];

if($cond=='insert')
{
   //insert statement
}
else if($cond=='edit')
{
 //edit statmement
}

else if($cond=='delete')
{
  //delete statement
}

OR

Code: Select all

if($cond=='insert')
{
   //insert statement
}

 if($cond=='edit')
{
 //edit statmement
}


 if($cond=='delete')
{
  //delete statement
}

Posted: Tue Aug 07, 2007 1:01 pm
by robshanks
Both are correct in that they both work as you say.

But the second example executes all three if statements even if the first is true.

The third option is a Switch statement:

Code: Select all

<?php
$cond=$_POST['cond'];
switch($cond)
{
  case "insert":
  //insert statement
  break;
  case "edit":
  //edit statement
  break;
  case "delete":
  //delete statement
  break;
  default:
  //default action
}

Posted: Tue Aug 07, 2007 4:33 pm
by VladSun
robshanks wrote: But the second example executes all three if statements even if the first is true.
One could understand this as "all if statetments are considered true" ...

@shivam0101
1. The usage of these statements depends higlhy on the context you are using it in. I could never tell what's best for you without having the surrounding code.
2. You could use "elseif" keyword instead of "else if".

Posted: Tue Aug 07, 2007 7:01 pm
by RobertGonzalez

Code: Select all

<?php
$cond = '';
if (!empty($_POST['cond'])) {
  $cond = $_POST['cond'];

  switch ($cond) {
    case 'insert':
      //insert statement
      break;

    case  'edit': 
      //edit statmement
      break;

    case 'delete':
      //delete statement
      break;

    default:
      // default if $cond is not one of these
}
?>

Posted: Tue Aug 07, 2007 7:03 pm
by superdezign
The reason it "works" is because it's not possible for those if statements to overlap. Using else and else if makes PHP not need to check all of the statements if one of them is true before it reaches the last one.

Posted: Tue Aug 07, 2007 7:07 pm
by VladSun
In some cases it is good to have all if statements executed.
Example:

Code: Select all

if ($action == "submit_edit")
{
	// do some validation

	if (!$dataLookingGood)
	{
		$action = "do_edit";
	}

	// do update
}

if ($action == "do_edit")
{
	// print Edit Form and fill it.
}

Posted: Tue Aug 07, 2007 7:12 pm
by RobertGonzalez
How is that good? PHP will have to actually run an if evaluation on both of them, even though one or the other is false. In an if else statement, if the if fires true, then the else is not evaluated. Less resource use is good.

Posted: Tue Aug 07, 2007 7:26 pm
by VladSun
Read more carefully what I've written ...
The advantage is reusing the code ...

Posted: Tue Aug 07, 2007 7:27 pm
by John Cartwright
That just seems like a nasty program flow. *step in OOP*

Posted: Tue Aug 07, 2007 7:28 pm
by VladSun
Jcart wrote:That just seems like a nasty program flow. *step in OOP*
It is :)

Posted: Tue Aug 07, 2007 7:30 pm
by superdezign
It's one of the few things an if statement can do that a switch can't. :-p

Posted: Tue Aug 07, 2007 7:43 pm
by VladSun
Sometimes it is very efficient to use "side effects". Yes, it is really very dangerous, nasty program flow, etc.
The example above is just like using switch without break in one or more case statements.

Posted: Tue Aug 07, 2007 9:08 pm
by Benjamin
VladSun wrote:Sometimes it is very efficient to use "side effects". Yes, it is really very dangerous, nasty program flow, etc.
The example above is just like using switch without break in one or more case statements.
I disagree. I would reword that and say, "Sometimes it is very efficient to not take shortcuts and write code the correct way and not worry about very dangerous, nasty logic etc. The example above is a programmer not using object oriented code to solve a problem".

Posted: Tue Aug 07, 2007 9:27 pm
by VladSun
...

One could even dream of having "if" object ...

Posted: Wed Aug 08, 2007 10:22 am
by RobertGonzalez
VladSun wrote:Yes, it is really very dangerous, nasty program flow, etc.
That makes me not want to use anything like this at all.