Solved

php if statement

Posted on 2011-02-16
4
844 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

739 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