IF-ELSEIF-ELSE statement in one line?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

IF-ELSEIF-ELSE statement in one line?

Post by lauthiamkok »

Hi,

I know that we can write IF-ELSE statement in one line, such as,

Code: Select all

$eatit = ($applet !="gree") ? "Eat" : "Dont Eat";
but how about IF-ELSEIF-ELSE statement??

how do I write these into one line?

Code: Select all

if(isset($cat_name))
{
$page_path = 'blog/categories/'.$cat_name;
}
elseif(isset($pg_archive)) 
{ 
$page_path =  'blog/archive/'.$pg_archive ; 
} 
else 
{
$page_path =  home';
}
many thanks,
Lau
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: IF-ELSEIF-ELSE statement in one line?

Post by Weirdan »

but how about IF-ELSEIF-ELSE statement??
how do I write these into one line?
Don't do that. Multiple lines are just ok, most of the time.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: IF-ELSEIF-ELSE statement in one line?

Post by lauthiamkok »

Weirdan wrote:
but how about IF-ELSEIF-ELSE statement??
how do I write these into one line?
Don't do that. Multiple lines are just ok, most of the time.
cool thanks for the advice. I will keep them then :D
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: IF-ELSEIF-ELSE statement in one line?

Post by AbraCadaver »

I totally agree with Weirdan, but to answer your question:

Code: Select all

$page_path = isset($cat_name) ? 'blog/categories/'.$cat_name : (isset($pg_archive) ? 'blog/archive/'.$pg_archive : 'home');
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: IF-ELSEIF-ELSE statement in one line?

Post by lauthiamkok »

AbraCadaver wrote:I totally agree with Weirdan, but to answer your question:

Code: Select all

$page_path = isset($cat_name) ? 'blog/categories/'.$cat_name : (isset($pg_archive) ? 'blog/archive/'.$pg_archive : 'home');
aww. thanks for this. i will keep it as a reference even though i have decided not to use it :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: IF-ELSEIF-ELSE statement in one line?

Post by John Cartwright »

I usually do

Code: Select all

$page_path =  'home'; //set the default first

if(isset($cat_name)) {
   $page_path = 'blog/categories/'.$cat_name;
} elseif(isset($pg_archive)) {
   $page_path =  'blog/archive/'.$pg_archive ;
}
Absolutely nothing wrong with using more lines.. in fact it usually will make your code more readable.
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: IF-ELSEIF-ELSE statement in one line?

Post by lauthiamkok »

John Cartwright wrote:I usually do

Code: Select all

$page_path =  'home'; //set the default first

if(isset($cat_name)) {
   $page_path = 'blog/categories/'.$cat_name;
} elseif(isset($pg_archive)) {
   $page_path =  'blog/archive/'.$pg_archive ;
}
Absolutely nothing wrong with using more lines.. in fact it usually will make your code more readable.
thank you for sharing your practise in writing this code. i agree with u that it is nothing wrong with using more lines :-)

thanks! :D
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: IF-ELSEIF-ELSE statement in one line?

Post by hypedupdawg »

Yes - I normally use that format. I would also advise you to use some sort of tab / sapcing structure, like the one below, where each statement progresses inwards:

Code: Select all

if ($contents[$i] == "end"){
	$vidspace = $_SERVER['DOCUMENT_ROOT'] . "/all/null.txt";
}else{
	$vidspace = "vidtrue.php";
	function choosesize ($var,$swidth){
		switch ($var){
			case "s":
				$val = 480;
				break;
			default:
				$val = 480;
				break;
			}
		return $val;
		}
	}
That way it is really easy to see which function you are inside, and how many braces you will need to close the statement, etc

It can be really useful when a function goes off the top of the page! :D
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: IF-ELSEIF-ELSE statement in one line?

Post by lauthiamkok »

hypedupdawg wrote:Yes - I normally use that format. I would also advise you to use some sort of tab / sapcing structure, like the one below, where each statement progresses inwards:

Code: Select all

if ($contents[$i] == "end"){
	$vidspace = $_SERVER['DOCUMENT_ROOT'] . "/all/null.txt";
}else{
	$vidspace = "vidtrue.php";
	function choosesize ($var,$swidth){
		switch ($var){
			case "s":
				$val = 480;
				break;
			default:
				$val = 480;
				break;
			}
		return $val;
		}
	}
That way it is really easy to see which function you are inside, and how many braces you will need to close the statement, etc

It can be really useful when a function goes off the top of the page! :D
a great practice again! it makes the code appears beautifully! I love it. well done! thanks :D
Post Reply