Passed by value and passed by reference in php

Passed by value and passed by reference in php

  • Different ways to write a PHP code
  • How to write comments in PHP ?
  • Introduction to Codeignitor (PHP)
  • How to echo HTML in PHP ?
  • Error handling in PHP
  • How to show All Errors in PHP ?
  • How to Start and Stop a Timer in PHP ?
  • How to create default function parameter in PHP?
  • How to check if mod_rewrite is enabled in PHP ?
  • Web Scraping in PHP Using Simple HTML DOM Parser
  • How to pass form variables from one page to other page in PHP ?
  • How to display logged in user information in PHP ?
  • How to find out where a function is defined using PHP ?
  • How to Get $_POST from multiple check-boxes ?
  • How to Secure hash and salt for PHP passwords ?
  • Program to Insert new item in array on any position in PHP
  • PHP append one array to another
  • How to delete an Element From an Array in PHP ?
  • How to print all the values of an array in PHP ?
  • How to perform Array Delete by Value Not Key in PHP ?
  • Removing Array Element and Re-Indexing in PHP
  • How to count all array elements in PHP ?
  • How to insert an item at the beginning of an array in PHP ?
  • PHP Check if two arrays contain same elements
  • Merge two arrays keeping original keys in PHP
  • PHP program to find the maximum and the minimum in array
  • How to check a key exists in an array in PHP ?
  • PHP | Second most frequent element in an array
  • Sort array of objects by object fields in PHP
  • PHP | Sort array of strings in natural and standard orders
  • How to pass PHP Variables by reference ?
  • How to format Phone Numbers in PHP ?
  • How to use php serialize() and unserialize() Function
  • Implementing callback in PHP
  • PHP | Merging two or more arrays using array_merge()
  • PHP program to print an arithmetic progression series using inbuilt functions
  • How to prevent SQL Injection in PHP ?
  • How to extract the user name from the email ID using PHP ?
  • How to count rows in MySQL table in PHP ?
  • How to parse a CSV File in PHP ?
  • How to generate simple random password from a given string using PHP ?
  • How to upload images in MySQL using PHP PDO ?
  • How to check foreach Loop Key Value in PHP ?
  • How to properly Format a Number With Leading Zeros in PHP ?
  • How to get a File Extension in PHP ?
  • How to get the current Date and Time in PHP ?
  • PHP program to change date format
  • How to convert DateTime to String using PHP ?
  • How to get Time Difference in Minutes in PHP ?
  • Return all dates between two dates in an array in PHP
  • Sort an array of dates in PHP
  • How to get the time of the last modification of the current page in PHP?
  • How to convert a Date into Timestamp using PHP ?
  • How to add 24 hours to a unix timestamp in php?
  • Sort a multidimensional array by date element in PHP
  • Convert timestamp to readable date/time in PHP
  • PHP | Number of week days between two dates
  • PHP | Converting string to Date and DateTime
  • How to get last day of a month from date in PHP ?
  • PHP | Change strings in an array to uppercase
  • How to convert first character of all the words uppercase using PHP ?
  • How to get the last character of a string in PHP ?
  • How to convert uppercase string to lowercase using PHP ?
  • How to extract Numbers From a String in PHP ?
  • How to replace String in PHP ?
  • How to Encrypt and Decrypt a PHP String ?
  • How to display string values within a table using PHP ?
  • How to write Multi-Line Strings in PHP ?
  • How to check if a String Contains a Substring in PHP ?
  • How to append a string in PHP ?
  • How to remove white spaces only beginning/end of a string using PHP ?
  • How to Remove Special Character from String in PHP ?
  • How to create a string by joining the array elements using PHP ?
  • How to prepend a string in PHP ?
Читайте также:  Html как задать селектор

Источник

What is Pass By Reference and Pass By Value in PHP?

In this article, we will learn about pass by value and pass by reference in PHP.

Now, let’s understand these two concepts in detail.

In PHP generally, we followed to pass the arguments to the function with passed by value approach. We are following this practice because if the value of the argument within the function is changed, it does not get changed outside of the function.

In some cases we may need to modify function arguments, So to allow a function to modify its arguments, they must be passed by reference.

Let’s begin with passed by reference. As it is already mentioned we can pass a variable by reference to a function so the function can modify the variable. To begin the process of passing the parameters passed by reference, prepend an ampersand (&) to the argument name in the function definition.

Example

Let’s test this with a simple example.

Output

Explanation

Here we have declared variable $a and passing it as pass by reference to the function calculate(). So as the principle says if the value of the $a gets changed inside the function then it will be also going to change outside the function.

Note

There is no reference sign on a function call — only on function definitions. Function definitions alone are enough to correctly pass the argument by reference. This is deprecated in 5.4 version of PHP when you use to calculate(&$a); it throws an error.

Example

Let’s test an example to understand pass by value.

Output

Explanation

Here we have passed the value to the function calculate() as pass by value. Its value gets changed inside the function but that is not reflected outside the function. The value of the variable remains the same outside the function.

Источник

PHP Passing by Reference

In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn’t change value of actual argument variable.

When arguments are passed by reference, change in value of formal argument is reflected in actual argument variable because the former is a reference to latter. Thus pass by reference mechanism helps in indirectly manipulating data in global space. It also helps in overcoming the fact that a function can return only one variable.

Pass by Value

In following example, two variables are passed to swap() function. Even though swapping mechanism takes place inside the function it doesn’t change values of variables that were passed

Example

"; $temp=$arg1; $arg1=$arg2; $arg2=$temp; echo "inside function after swapping: arg1=$arg1 arg2=$arg2
"; > $arg1=10; $arg2=20; echo "before calling function : arg1=$arg1 arg2=$arg2
"; swap($arg1, $arg2); echo "after calling function : arg1=$arg1 arg2=$arg2
"; ?>

Output

This example gives following output

before calling function : arg1=10 arg2=20 inside function before swapping: arg1=10 arg2=20 inside function after swapping: arg1=20 arg2=10 after calling function : arg1=10 arg2=20

Pass by reference

In order to receive arguments by reference, variable used formal argument must be prefixed by & symbol. It makes reference to variables used for calling the function. Hence, result of swapping inside function will also be reflected in original variables that were passed

Example

"; $temp=$arg1; $arg1=$arg2; $arg2=$temp; echo "inside function after swapping: arg1=$arg1 arg2=$arg2
"; > $arg1=10; $arg2=20; echo "before calling function : arg1=$arg1 arg2=$arg2
"; swap($arg1, $arg2); echo "after calling function : arg1=$arg1 arg2=$arg2
"; ?>

Output

Result of swapping will be shown as follows

before calling function : arg1=10 arg2=20 inside function before swapping: arg1=10 arg2=20 inside function after swapping: arg1=20 arg2=10 after calling function : arg1=20 arg2=10

In following example, array element are references to individual variables declared before array initialization. If element is modified, value of variable also changes

Example

Output

Values of $a, $b and $c also get incremented

It is also possible to pass by reference an array to a function

Example

 $arr=[1,2,3,4,5]; arrfunction($arr); foreach ($arr as $i) echo $i . " "; ?>

Output

Modified array will be displayed as follows

Object and reference

In PHP, objects are passed by references by default. When a reference of object is created, its reference is also sent as argument in the form of $this which is also reference to first object

Example

name; > function setname($name)< $this->name=$name; > > $obj1=new test1(); $obj2=&$obj1; $obj1->setname("Amar"); echo "name: " .$obj2->getname(); ?>

Output

Above code will display following output

Источник

Оцените статью