Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , ,

Sponsored Links



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

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2010
- Empty Shell -'s Avatar
Member
 
Join Date: Aug 2006
Posts: 112
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
Reply With Quote
  #2  
Old 06-03-2010
Warner's Avatar
Member
 
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.
Reply With Quote
  #3  
Old 06-03-2010
Viensterrr's Avatar
Member
 
Join Date: Jul 2006
Posts: 232
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
Reply With Quote
  #4  
Old 06-03-2010
Solitario's Avatar
Member
 
Join Date: Aug 2006
Posts: 220
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.
Reply With Quote
  #5  
Old 06-03-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
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.!!
Reply With Quote
  #6  
Old 06-03-2010
Member
 
Join Date: May 2008
Posts: 1,990
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.
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to use an Alternative Syntax for Control Structures in PHP?"
Thread Thread Starter Forum Replies Last Post
Alternative to change the mouse speed and control of Pirates of Black Cove Gaharwar Video Games 5 02-09-2011 11:39 AM
Can I use Belkin Bluetooth receiver as an alternative for volume control Dinesh Dubey Windows Software 5 07-03-2011 12:01 AM
How does a class vary from Structures ? Chellam Software Development 4 30-12-2010 08:20 AM
ATi Catalyst 9.1 or 9.2 with an alternative for Catalyst Control Center Gaige Monitor & Video Cards 6 03-06-2010 12:03 AM
Pointer to an array of structures Zool Software Development 3 13-05-2009 11:51 PM


All times are GMT +5.5. The time now is 12:07 PM.