smartguyin Posted January 28, 2007 Share Posted January 28, 2007 I am using a function in my script like this one ...function setup_page ($title) {echo "<html><head><title>My Database $title</title>";}I want to echo the below if there is no function variable used.. in above func... [b]setup_page ($title)[/b] comes from another page soo if there is no [b]setup_page ($title)[/b] my scirpt should echo the below[b]echo "<html><head><title>NO TITLE</title>";[/b] Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/ Share on other sites More sharing options...
Orio Posted January 28, 2007 Share Posted January 28, 2007 Default values is what you are looking for. See [url=http://www.php.net/manual/en/functions.arguments.php]this[/url] page for more info (scroll down to default argument values).This is how it's done:[code]function setup_page ($title = "Untitled"){echo "<html><head><title>My Database $title</title>";}[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171097 Share on other sites More sharing options...
smartguyin Posted January 28, 2007 Author Share Posted January 28, 2007 please explain me i am still not able to use the ELSE IF STATEMENT.... sooo if there is no $title variable sent by another script then it should echo the other thing..please help Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171277 Share on other sites More sharing options...
.josh Posted January 28, 2007 Share Posted January 28, 2007 [code]function setup_page ($title) { $title = (isset($title) || trim($title) != 0) ? "My Database $title" : "NO TITLE"; echo "<html><head><title>$title</title>";}[/code]but orio's is better. kinda. I guess it depends on where the $title data is coming from. Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171284 Share on other sites More sharing options...
smartguyin Posted January 28, 2007 Author Share Posted January 28, 2007 no still not getting it [b]NO TITLE[/b] the variable is coming from body.php which is place in index.php after header.php Soo if some of the pages which the variable is not set... then i wanna echo [b]NO TITLE[/b]Thanks for helping but still i am not getting it Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171292 Share on other sites More sharing options...
corbin Posted January 28, 2007 Share Posted January 28, 2007 English isn't your first language, is it?[code]function setup_page ($title = 'NO TITLE') {echo "<html><head><title>";if($title != 'NO TITLE') { echo "My Database $title"; } else { echo $title; }echo "</title>";}[/code]That should work... Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171294 Share on other sites More sharing options...
alpine Posted January 28, 2007 Share Posted January 28, 2007 This should work, look at the examples below:[code]<?phpfunction setup_page($title = false){if($title == false) $title = "NO TITLE";echo "<html><head><title>My Database $title</title>";}setup_page($title); // result: NO TITLE$title = "";setup_page($title); // result: NO TITLE$title = "foo";setup_page($title); // result: foo?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171299 Share on other sites More sharing options...
smartguyin Posted January 28, 2007 Author Share Posted January 28, 2007 No NOT yet... thanks for your help..ok tell me if no function is set for title like setup_page("Search Page"); then how can i echo NO TITLE Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171321 Share on other sites More sharing options...
.josh Posted January 28, 2007 Share Posted January 28, 2007 All of the suggestions here should work. If it's not working, then your problem is somewhere else. You're going to have to post some other code. For instance, post the portion of your code and surrounding lines, where you call this function, including where you assign the data that's being passed to the function. Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171384 Share on other sites More sharing options...
alpine Posted January 28, 2007 Share Posted January 28, 2007 Agree on something else being your problem if you have tried all solutions here, but not all of these suggestions work (on my server anyhow)A simplified collection of the proposed soulutions, just one works IF $title is set as "", two of them works if $title is not defined at all[code]<?phpfunction setup_page1($title = false){if($title == false) $title = "NO TITLE";echo $title ."<br />";}function setup_page2($title = "Untitled"){echo $title ."<br />";}function setup_page3($title){$title = (isset($title) || trim($title) != 0) ? "$title" : "NO TITLE";echo $title ."<br />";}$title = "";setup_page1($title); // prints "NO TITLE"setup_page2($title); // prints nothingsetup_page3($title); // prints nothing - but if $title is absent, this one works?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171442 Share on other sites More sharing options...
smartguyin Posted January 29, 2007 Author Share Posted January 29, 2007 CAN I DO SOME THING LIKE THIS...if (!function setup_page($title)) {echo "NO TITLE";}i know this code is worng help me with this to use if else statement Quote Link to comment https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171646 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.