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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.