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
lauthiamkok
Forum Contributor
Posts: 153 Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom
Post
by lauthiamkok » Thu May 13, 2010 1:11 pm
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
Weirdan
Moderator
Posts: 5978 Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine
Post
by Weirdan » Thu May 13, 2010 1:39 pm
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
Post
by lauthiamkok » Thu May 13, 2010 2:09 pm
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
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Thu May 13, 2010 3:15 pm
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
Post
by lauthiamkok » Thu May 13, 2010 3:27 pm
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
John Cartwright
Site Admin
Posts: 11470 Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:
Post
by John Cartwright » Thu May 13, 2010 5:24 pm
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
Post
by lauthiamkok » Thu May 13, 2010 5:54 pm
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!
hypedupdawg
Forum Commoner
Posts: 74 Joined: Sat Apr 10, 2010 5:21 am
Post
by hypedupdawg » Fri May 14, 2010 2:27 am
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!
lauthiamkok
Forum Contributor
Posts: 153 Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom
Post
by lauthiamkok » Fri May 14, 2010 10:26 am
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