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
Using Named Parameters in PHP
PHP

Using Named Parameters in PHP

InfinityCoder November 21, 2016

You want to specify your arguments to a function by name, instead of simply their position in the function invocation.

PHP doesn’t have language-level named parameter support like some other languages do.

However, you can emulate it by having a function use one parameter and making that parameter an associative array:

1
2
3
4
5
6
7
8
9
10
11
function image($img) {
   $tag = '<img src="' . $img['src'] . '" ';
   $tag .= 'alt="' . (isset($img['alt']) ? $img['alt'] : '') .'"/>';
   return $tag;
}
 
// $image1 is '<img src="cow.png" alt="cows say moo"/>'
$image1 = image(array('src' => 'cow.png', 'alt' => 'cows say moo'));
 
// $image2 is '<img src="pig.jpeg" alt=""/>'
$image2 = image(array('src' => 'pig.jpeg'));

Though using named parameters makes the code inside your functions more complex, it ensures the calling code is easier to read.

Because a function lives in one place but is called in many, this makes for more understandable code.
Because you’ve abstracted function parameters into an associative array, PHP can’t warn you if you accidentally misspell a parameter’s name.

You need to be more careful because the parser won’t catch these types of mistakes. Also, you can’t take advantage of PHP’s ability to assign a default value for a parameter.

Luckily, you can work around this deficit with some simple code at the top of the function:

1
2
3
4
5
6
7
function image($img) {
    if (! isset($img['src']))     { $img['src']     = 'cow.png';      }
    if (! isset($img['alt']))     { $img['alt']     = 'milk factory'; }
    if (! isset($img['height']))  { $img['height']  = 100;            }
    if (! isset($img['width']))   { $img['width']   = 50;             }
    /* ... */
}

Using the isset() function, check to see if a value for each parameter is set; if not, assign a default value.
Alternatively, you can use array_merge() to handle this:

1
2
3
4
5
6
7
8
9
function image($img) {
   $defaults = array('src' => 'cow.png',
                     'alt' => 'milk factory',
                     'height' => 100,
                     'width' => 50
                    );
   $img = array_merge($defaults, $img);
   /* ... */
}

If the same key exists in the arrays passed to array_merge(), then it uses the value in the later array. In the preceding example, that means that any values in $img override values in $defaults.

But if a key is missing from $img, the value from $defaults is used.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Validating Form Input: Email Addresses in PHP
You want to know whether an email address a user …

Validating Form Input: Email Addresses in PHP

Finding the Current Date and Time in PHP
You want to know what the time or date is. …

Finding the Current Date and Time 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