0

Possible Duplicate:
Difference between if () { } and if () : endif;

Simple question,

When I started programming PHP I was shown my if statements like this:

If(1 == 1):
Echo 'hello world';
Endif;

Where as most people prefer

If(1== 1) {
Echo 'hello world';
}

Is there any difference? Does is improve the speed of the script or is it Better than the way I do it?

Community
  • 1
  • 1
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73

5 Answers5

2

The statement are equals though for a better legibility in a Model View Controller project is better to use

if(1== 1) {
    echo 'hello world';
}

in model/controller part and the other one in the View part.

<? if(1 == 1): ?>
   <div>..</div>
<? endif;?>

so a web designer/ graphic can better handle html code.

Gianpaolo Di Nino
  • 1,139
  • 5
  • 17
1

No. However, you should not think about micro optimization (it's the root of all evil), especially since you name yourself a beginner.

The second one is more common, the first one is often more readable when mixing php and html.

KingCrunch
  • 128,817
  • 21
  • 151
  • 173
1

There is no speed difference between them. This is an alternate syntax of if.

Some people prefer if() { or some prefer if ():

I personally use if (): when there is a bunch of HTML need to output.

<?php if (condition) : ?>
//some html tags html
<?php endif; ?>

and I use if(condition) { when some php processing to be done.

<?php
if (condition)
{
   //other PHP stuff
}
?>
Shakti Singh
  • 84,385
  • 21
  • 134
  • 153
1

Personally I use alternative syntax when I mix PHP with HTML (its much cleaner this way for me):

    <p>
        <label>Customer:</label>
        <?php echo Form::input('customer', Arr::get($post, 'customer'), array('maxlength' => 80)) ?>
        <?php if (isset($errors['customer'])): ?>
            <span class="error"><?php echo $errors['customer'] ?></span>
        <?php endif ?>
    </p>

Other then that there is no difference.

matino
  • 17,199
  • 8
  • 49
  • 58
1

Is there any difference? Does is improve the speed of the script or is it Better than the way I do it?

No.
That's just two different ways to do the same thing.

For the second one almost every good code editor/IDE will highlight the matching brace.
Such a reason, along with the fact that curly braces are compatible with many other languages, makes them used more often.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345