• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 851
  • Last Modified:

php if statement

is it correct to write it like this?
what do you recommend?

if empty($datalanguage)
      {
      $datalanguage="0";
      }
      else
      {
      $datalanguage= $datalanguage;
      }
0
Braveheartli
Asked:
Braveheartli
  • 2
3 Solutions
 
Beverley PortlockCommented:
IF needs brackets around the expression,

if ( empty($datalanguage) )
      {
      $datalanguage="0";
      }
      else
      {
      $datalanguage= $datalanguage;
      }
0
 
KalpanCommented:
please modify

if (!isset($datalanguage) || empty($datalanguage))
{
     $datalanguage="0";
}else      {
      $datalanguage= $datalanguage;
 }
0
 
Beverley PortlockCommented:
On an additional matter, code styling..... the IF layout you are using is uncommon but not unknown. It is considered "better" to align the ELSE with its matching IF so that it is obvious that they are related. If you nest multiple IF statements the reason for this becomes obvious. So rather than

if ( empty($datalanguage) )
      {
      $datalanguage="0";
      }
      else
      {
      $datalanguage= $datalanguage;
      }


More like this

if ( empty($datalanguage) )
      {
      $datalanguage="0";
      }
else
      {
      $datalanguage= $datalanguage;
      }


or (my preference)

if ( empty($datalanguage) )  {
      $datalanguage="0";
}
else  {
      $datalanguage= $datalanguage;
}


and for single statements it is not unusual to skip the compounding braces


if ( empty($datalanguage) )  
      $datalanguage="0";
else  
      $datalanguage= $datalanguage;
0
 
BraveheartliMarketingAuthor Commented:
thank you
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now