1
kaotik
Tutorial: Alternative Syntax
  • 2010/5/6 11:43

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


Alternative syntax

We are all used to writing code like this:
if ($a==1){
echo 
'hello world';
}


Now what if you have a lot of dependent code:
if ($a==1){
switch (
$i) {
    case 
0:
        echo 
"i equals 0";
        break;
    case 
1:
        echo 
"i equals 1";
        break;
}
}


Understanding the end brackets can become dificult. Php provides a solution called alternative syntax.

This same code could be written:
if ($a==1):
switch (
$i) :
    case 
0:
        echo 
"i equals 0";
        break;
    case 
1:
        echo 
"i equals 1";
        break;
endswitch;
endif;


I'm not stating it's better or worse then the common way

2
ghia
Re: Tutorial: Alternative Syntax
  • 2010/5/6 12:09

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


I think we better stick to that standard xoops syntax.

The recent cleanups and polishings of the core code make it already unnecessary difficult to compare versions.
The fact that once it was formatted in one way and then again reversed, make it even more enoying.

If there is proper indention, the control structure stays readable.
if ($a==1){
  switch (
$i) {
    case 
0:
        echo 
"i equals 0";
        break;
    case 
1:
        echo 
"i equals 1";
        break;
  }
}


Personaly, I find the aligned bracket system better:
if ($a==1)
{
  switch (
$i)
  {
    case 
0:
        echo 
"i equals 0";
        break;
    case 
1:
        echo 
"i equals 1";
        break;
  }
}
else
{
  echo 
"a is not 1";
}

3
kaotik
Re: Tutorial: Alternative Syntax
  • 2010/5/6 13:45

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


This method does exist in PHP and each person is free to code his/her module as they see fit.
I like to teach people and show them the available options. It's then up to them to analyse the pro's and con's of each alternative.

I can see your reasoning for the core. I'm sure standard methods won't be affected by my small tutorial.
Programing is an ever changing profession and learning new methods is an important part of it.

4
ghia
Re: Tutorial: Alternative Syntax
  • 2010/5/6 14:52

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


Of course everybody is free to do it as they like, but for easy exchange and readability it is recommended to follow the XOOPS Standard coding practices.

The alternative syntax is not realy a new programming method, but a style figure. Interesting to know, but no real advancement.

Login

Who's Online

60 user(s) are online (36 user(s) are browsing Support Forums)


Members: 0


Guests: 60


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Jul 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits