InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

Menu
  • Home
  • Sitemap

Python Programming Language Best Tutorials and Code Examples

Learn Python Right Now!
Home
PHP
Setting Default Values for Function Parameters in PHP
PHP

Setting Default Values for Function Parameters in PHP

InfinityCoder November 21, 2016

You want a parameter to have a default value if the function’s caller doesn’t pass it. For example, a function to wrap text in an HTML tag might have a parameter for the tag name, which defaults to strong if none is given.

Assign the default value to the parameters inside the function prototype:

1
2
3
function wrap_in_html_tag($text, $tag = 'strong') {
    return "<$tag>$text</$tag>";
}

The example in the Solution sets the default tag value to strong. For example:

1
print wrap_in_html_tag("Hey, a mountain lion!");

prints:

1
<strong>Hey, a mountain lion!</strong>

This example:

1
print wrap_in_html_tag("Look over there!", "em");

prints:

1
<em>Look over there!</em>

There are two important things to remember when assigning default values. First, all parameters with default values must appear after parameters without defaults.

Otherwise, PHP can’t tell which parameters are omitted and should take the default value and which arguments are overriding the default. So wrap_in_html_tag() can’t be defined as:

1
function wrap_in_html_tag($tag = 'strong', $text)

If you do this and pass wrap_in_html_tag() only a single argument, PHP assigns the value to $tag and issues a warning complaining of a missing second argument.
Second, the assigned value must be a constant, such as a string or a number. It can’t be a variable. Again, using wrap_in_html_tag(), such as our example, you can’t do this:

1
2
3
4
5
$my_favorite_html_tag = 'blink';
 
function wrap_in_html_tag($text, $tag = $my_favorite_html_tag) {
    return "<$tag>$text</$tag>";
}

If you want to assign a default of nothing, one solution is to assign the empty string to your parameter:

1
2
3
4
function wrap_in_html_tag($text, $tag = '') {
    if (empty($tag)) { return $text; }
    return "<$tag>$text</$tag>";
}

This function returns the original string, if no value is passed in for the $tag. If a nonempty tag is passed in, it returns the string wrapped inside of tags.
Depending on circumstances, another option for the $tag default value is either 0 or NULL.

In wrap_in_html_tag(), you don’t want to allow an empty-valued tag. However, in some cases, the empty string can be an acceptable option.

As the following code shows, you can use a default message if no argument is provided but an empty message
if the empty string is passed:

1
2
3
4
5
6
function log_db_error($message = NULL) {
    if (is_null($message)) {
       $message = "Couldn't connect to DB";
    }
       error_log("[DB] [$message]");
}

 

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Checking If an Object Is an Instance of a Specific Class in PHP
You want to check if an object is an instance …

Checking If an Object Is an Instance of a Specific Class in PHP

Program: Finding Fresh Links in PHP
Example 13-23 is a modification of the program in Example …

Program: Finding Fresh Links in PHP

About The Author

InfinityCoder
InfinityCoder

Leave a Reply

Cancel reply

Recent Tutorials InfinityQuest

  • Adding New Features to bash Using Loadable Built-ins in bash
    Adding New Features to bash Using Loadable …
    June 27, 2017 0
  • Getting to the Bottom of Things in bash
    Getting to the Bottom of Things in …
    June 27, 2017 0

Recent Comments

  • fer on Turning a Dictionary into XML in Python
  • mahesh on Turning a Dictionary into XML in Python

Categories

  • Bash
  • PHP
  • Python
  • Uncategorized

InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more

About Us

Start learning your desired programming language with InfinityQuest.com.

On our website you can access any tutorial that you want with video and code examples.

We are very happy and honored that InfinityQuest.com has been listed as a recommended learning website for students.

Popular Tags

binary data python CIDR convert string into datetime python create xml from dict python dictionary into xml python how to create xml with dict in Python how to write binary data in Python IP Address read binary data python tutorial string as date object python string to datetime python

Archives

  • June 2017
  • April 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
Copyright © 2021 InfinityQuest - Programming Code Tutorials and Examples with Python, C++, Java, PHP, C#, JavaScript, Swift and more
Programming Tutorials | Sitemap