Elitmiar
Elitmiar

Reputation: 36879

What are the different ways of writing "if" conditional statements using PHP?

What are the different ways of writing if conditional statements using PHP?

I know for example of the following

if($test == 1){
}else{
}

and

if($test == 1)
   echo 'asdsa';
else
   echo 'sdaaa';

Upvotes: 2

Views: 7699

Answers (8)

Ewan Todd
Ewan Todd

Reputation: 7312

Don't forget that "Conditional Complexity" is a code smell. Polymorphism is your friend.

Conditional logic is innocent in its infancy, when it’s simple to understand and contained within a few lines of code. Unfortunately, it rarely ages well. You implement several new features and suddenly your conditional logic becomes complicated and expansive. [Joshua Kerevsky: Refactoring to Patterns]

One of the simplest things you can do to avoid nested if blocks is to learn to use Guard Clauses. (Note: this is not PHP syntax. Regard it as pseudo code. The techniques here are what are important.)

double getPayAmount() {
if (_isDead) return deadAmount();
if (_isSeparated) return separatedAmount();
if (_isRetired) return retiredAmount();
return normalPayAmount();
};  

The other thing I have found simplifies things pretty well, and which makes your code self-documenting, is Consolidating conditionals.

double disabilityAmount() {
    if (isNotEligableForDisability()) return 0;
    // compute the disability amount

Other valuable refactoring techniques associated with conditional expressions include Decompose Conditional, Replace Conditional with Visitor, and Reverse Conditional.

Now that you have some new hammers, don't let everything look like a nail!

Upvotes: 4

Nathan Kleyn
Nathan Kleyn

Reputation: 5143

The standard if statement:

if(expression) {
    // code
} elseif(expression) {
    // code
} else {
    // code
}

Without braces for single lines of code after each statement:

if(expression)
    // single line of code
elseif(expression)
    // single line of code
else
    // single line of code

The alternate control syntax:

if(expression):
    // code
elseif(expression):
    // code
else:
    // code
endif;

And finally, the ternary operator:

(expression ? expression_if_true : expression_if_false);

Which can also be written as:

(expression) ? expression_if_true : expression_if_false;

Or without brackets entirely if you wish.

Upvotes: 4

Milan Babuškov
Milan Babuškov

Reputation: 61158

echo ($test == 1) ? 'asdsa' : 'sdaaa';

Upvotes: 7

Tom
Tom

Reputation: 7223

Besides what's already been said, there's stuff like

$sql_link = mysql_connect('localhost', 'root') or die('no mysql');

Or like Alternative syntax for control structures

(the assignment "OR" trick is really a trick :) if the mysql_connect() doesn't evaulate to true, PHP will try to evaluate the second expression so this is really a hack of:

if (mysql_connect('localhost', 'root')) {
    $sql_link = true;
}
else {
    die('no mysql');
}

)

Upvotes: 5

dnagirl
dnagirl

Reputation: 20446

Straight from the PHP manual:

<?php
if ($a == 5):
    echo "a equals 5";
    echo "...";
elseif ($a == 6):
    echo "a equals 6";
    echo "!!!";
else:
    echo "a is neither 5 nor 6";
endif;
?> 

Upvotes: 2

Greg
Greg

Reputation: 321786

There's the alternative control structure syntax:

if ($text == 1):
    echo 'asdsa';
else:
   echo 'asdsa';
endif;

Upvotes: 12

Lizard
Lizard

Reputation: 45022

if($test == 1){
}else{
}

# can only be used if performing 1 line of code after statement
if($test == 1)
   echo 'asdsa';
else
   echo 'sdaaa';

#you can have as many elseif as you like (but you may wish to check out switch see below:
if($test == 1){
}elseif{
}else{
}

Also take a look at switch() http://php.net/manual/en/control-structures.switch.php

switch($test)
{
    case "1" :
        break;
    case "2" :
        break;
    default :
        break;
}

Upvotes: 3

Thomas Owens
Thomas Owens

Reputation: 116187

The other way is to use a ternary operator. The example in the PHP documentation is:

<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

?>

The other control structures are documented in the PHP manual as well. The only conditional statements are the ternary conditional operator, if (and else), and elseif or else if. However, they do have an alternative syntax.

Upvotes: 9

Related Questions