xoops forums

kaotik

Just can't stay away
Posted on: 2010/5/6 11:43
kaotik
kaotik (Show more)
Just can't stay away
Posts: 861
Since: 2004/2/19
#1

Tutorial: Alternative Syntax

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

ghia

Community Support Member
Posted on: 2010/5/6 12:09
ghia
ghia (Show more)
Community Support Member
Posts: 4954
Since: 2008/7/3 1
#2

Re: Tutorial: Alternative Syntax

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";
}

kaotik

Just can't stay away
Posted on: 2010/5/6 13:45
kaotik
kaotik (Show more)
Just can't stay away
Posts: 861
Since: 2004/2/19
#3

Re: Tutorial: Alternative Syntax

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.

ghia

Community Support Member
Posted on: 2010/5/6 14:52
ghia
ghia (Show more)
Community Support Member
Posts: 4954
Since: 2008/7/3 1
#4

Re: Tutorial: Alternative Syntax

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.