Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php if statement

Posted on 2011-02-16
4
Medium Priority
?
849 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

618 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