Page 1 of 1
IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 1:11 pm
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
Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 1:39 pm
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.
Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 2:09 pm
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

Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 3:15 pm
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');
Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 3:27 pm
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

Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 5:24 pm
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.
Re: IF-ELSEIF-ELSE statement in one line?
Posted: Thu May 13, 2010 5:54 pm
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!

Re: IF-ELSEIF-ELSE statement in one line?
Posted: Fri May 14, 2010 2:27 am
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!

Re: IF-ELSEIF-ELSE statement in one line?
Posted: Fri May 14, 2010 10:26 am
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!

a great practice again! it makes the code appears beautifully! I love it. well done! thanks
