?
Solved

php if statement

Posted on 2011-02-16
4
Medium Priority
?
846 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 1400 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 600 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 1400 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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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 i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

762 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