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
Accessing Function Parameters in PHP
PHP

Accessing Function Parameters in PHP

InfinityCoder November 21, 2016

You want to access the values passed to a function.

Use the names from the function prototype:

1
2
3
4
5
6
7
8
9
10
function commercial_sponsorship($letter, $number) {
    print "This episode of Sesame Street is brought to you by ";
    print "the letter $letter and number $number.\n";
}
 
commercial_sponsorship('G', 3);
 
$another_letter = 'X';
$another_number = 15;
commercial_sponsorship($another_letter, $another_number);

Inside the function, it doesn’t matter whether the values are passed in as strings, numbers, arrays, or another kind of variable. You can treat them all the same and refer to them using the names from the prototype.
Unless otherwise specified, all non-object values being passed into and out of a function are passed by value, not by reference. (By default, objects are passed by reference.)

This means PHP makes a copy of the value and provides you with that copy to access and manipulate.

Therefore, any changes you make to your copy don’t alter the original value.
For example:

1
2
3
4
5
6
7
function add_one($number) {
    $number++;
}
 
$number = 1;
add_one($number);
print $number;

prints:

1
1

If the variable had been passed by reference, the value of $number in the global scope would have been 2.
In many languages, passing variables by reference has the additional benefit of being significantly faster than passing them by value.

Although passing by reference is faster in PHP, the speed difference is marginal. For that reason, we suggest passing variables by reference only when actually necessary and never as a performance-enhancing trick.

Share
Tweet
Email
Prev Article
Next Article

Related Articles

Localizing Images in PHP
You want to display images that have text in them …

Localizing Images in PHP

Creating a Resource in PHP
You want to let people add a new resource to …

Creating a Resource 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