if else or if statement

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shivam0101
Forum Contributor
Posts: 197
Joined: Sat Jun 09, 2007 12:09 am

if else or if statement

Post 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
}
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post 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
}
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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".
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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
}
?>
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
}
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Read more carefully what I've written ...
The advantage is reusing the code ...
Last edited by VladSun on Tue Aug 07, 2007 9:35 pm, edited 2 times in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

That just seems like a nasty program flow. *step in OOP*
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

Jcart wrote:That just seems like a nasty program flow. *step in OOP*
It is :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

It's one of the few things an if statement can do that a switch can't. :-p
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Post 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".
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

...

One could even dream of having "if" object ...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
Post Reply