Jump to content

Fucntions


smartguyin

Recommended Posts

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]
Link to comment
https://forums.phpfreaks.com/topic/36052-fucntions/
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171097
Share on other sites

This should work, look at the examples below:
[code]

<?php

function 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]
Link to comment
https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171299
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171384
Share on other sites

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]

<?php

function 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 nothing
setup_page3($title); // prints nothing - but if $title is absent, this one works

?>

[/code]
Link to comment
https://forums.phpfreaks.com/topic/36052-fucntions/#findComment-171442
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.