Solved

php if statement

Posted on 2011-02-16
4
841 Views
Last Modified: 2012-06-21
is it correct to write it like this?
what do you recommend?

if empty($datalanguage)
      {
      $datalanguage="0";
      }
      else
      {
      $datalanguage= $datalanguage;
      }
0
Comment
Question by:Braveheartli
  • 2
4 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 350 total points
ID: 34904442
IF needs brackets around the expression,

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

Assisted Solution

by:Kalpan
Kalpan earned 150 total points
ID: 34904448
please modify

if (!isset($datalanguage) || empty($datalanguage))
{
     $datalanguage="0";
}else      {
      $datalanguage= $datalanguage;
 }
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 350 total points
ID: 34904460
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
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 34904552
thank you
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now