Results 1 to 6 of 6

Thread: How to use an Alternative Syntax for Control Structures in PHP?

  1. #1
    Join Date
    Aug 2006
    Posts
    122

    How to use an Alternative Syntax for Control Structures in PHP?

    Hi everyone,
    I have recently started with the PHP programming language. I have just finished with some simple loops that are used in PHP coding. I came to know that there is an alternate way for the syntax of the control structures. But I don't know anything more than this.!! So thought that you guys are better for explaining the topics. Please explain me how to use an Alternative Syntax for Control Structures in PHP?
    Thanks for Sparing the Time (Advance).
    As you simplify your life, the laws of the universe will be simpler; solitude will not be solitude, poverty will not be poverty, nor weakness.Henry David Thoreau

  2. #2
    Join Date
    Mar 2008
    Posts
    349

    Re: How to use an Alternative Syntax for Control Structures in PHP?

    You should know that PHP offers another way to gather statements inside a block for control functions if, while, for, foreach, and switch. In each case, the principle is to replace the opening brace by two colon ( : ) and the closing brace to endif;, endwhile;, endfor;, endforeach; or endcase. All PHP scripts are a sequence of instructions. A statement can be an assignment, a function call, a conditional statement or a statement that does nothing (an empty statement). A statement usually ends with a semicolon ( ";"). In addition, statements can be grouped into block delimited by curly braces ( "()"). A block is considered as an instruction.

  3. #3
    Join Date
    Jul 2006
    Posts
    289

    Re: How to use an Alternative Syntax for Control Structures in PHP?

    PHP offers an alternative syntax for some of its control structures. In the below example, the HTML block "A is equal to 4" is nested within an if using this new syntax. This HTML code will be displayed if the variable $ a is equal to 4.
    PHP Code:
    <?php if ($a == 4): ?>
    A is equal to 4
    <?php endif; ?>
    Signatures reduce available bandwidth

  4. #4
    Join Date
    Aug 2006
    Posts
    227

    Re: How to use an Alternative Syntax for Control Structures in PHP?

    I am giving you an alternate syntax for the example provided by the 'Viensterrr' which applies to else and elseif as well. The following is an if structure with elseif and else in the alternative format :
    PHP Code:
    <?php
    if ($a == 4):
        echo 
    "a equals 4";
        echo 
    "...";
    elseif (
    $a == 5):
        echo 
    "a equals 5";
        echo 
    "!!!";
    else:
        echo 
    "a is neither 4 nor 5";
    endif;
    ?>
    I do to dead flowers what people at morgues do to dead people. Suck all the moisture out, dip them in plastic, paint them up pretty and put them in a nice frame.

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    Re: How to use an Alternative Syntax for Control Structures in PHP?

    You can not use different syntax in the same control block. When you going to make a script , you must try easiest way to do and fastest way to parse. Using alternative-syntax is very useful to shorten your code. The end_; structure sometimes makes it easier to tell which block statement end you are looking at. It's much harder to tell which nested block a } belongs to than an end_;. Hope that you got the point I was trying to explain.!!

  6. #6
    Join Date
    May 2008
    Posts
    2,012

    Re: How to use an Alternative Syntax for Control Structures in PHP?

    There are many times that this alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them. Interface templates are very often in need of this. This is very useful when one person is programmer and an another person is modifying the HTML. Clear separation in such cases is extremely useful.

Similar Threads

  1. Replies: 5
    Last Post: 02-09-2011, 10:39 AM
  2. How does a class vary from Structures ?
    By Chellam in forum Software Development
    Replies: 4
    Last Post: 30-12-2010, 08:20 AM
  3. Replies: 6
    Last Post: 02-06-2010, 11:03 PM
  4. Pointer to an array of structures
    By Zool in forum Software Development
    Replies: 3
    Last Post: 13-05-2009, 10:51 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,239,258.75169 seconds with 17 queries