How to Call by Value & Call by Reference in PHP with Example

View: 290    Dowload: 0   Comment: 0   Post by: hanhga   Category: Php&mySql   Fields: Other

File has been tested

What’s the difference between call by value & call by reference in PHP. This is the most important concept used in programming and sometime asked in interviews. Don’t worry if you are new to this terminology, you’ll learn this concept in this tutorial.

Call by Value & Call by Reference in PHP

Let’s understand this concept using some programming examples.

function increment($num)
{
 
 $num = $num + 1;
 echo $num."\n";
}
 
$n = 1;
 
increment($n); /* Output 2 */
 
echo $n; /* Output 1 */

Now again write this program with minimal modification in argument.

function increment(&$num)
{
 
 $num = $num + 1;
 echo $num."\n";
}
 
$n = 1;
 
increment($n); /* Output 2 */
 
echo $n; /* Output 2 */

Let’s understand the meaning of call by value & call by reference.

Call by Value : In call by value if the argument within the function is changed, it doesn’t get changed outside of the function. As we seen in our first example.

Call by Reference : In call by reference we prepend an ampersand (&) to the argument name in the function definition. In passing value by reference can modify the variable value as we seen our second example.

Swap Two Numbers using Call By Value in PHP

function swap($first, $second)
{
 $temp = $first;
 $first = $second;
 $second = $temp;
 
 echo "First number ".$first." Second number ".$second."\n";
}
 
$a = 5;
$b = 7;
 
swap ($a, $b); /* Output : First number 7 Second number 5 */
 
echo "First number ".$a." Second number ".$b; /* Output : First number 5 Second number 7 */

Swap Two Numbers using Call By Reference in PHP

function swap(&$first, &$second)
{
$temp = $first;
$first = $second;
$second = $temp;
echo "First number ".$first." Second number ".$second."\n";
}
$a = 5;
$b = 7;
swap ($a, $b); /* Output : First number 7 Second number 5 */
echo "First number ".$a." Second number ".$b; /* Output : First number 7 Second number 5 */

How to Call by Value & Call by Reference in PHP with Example

What’s the difference between call by value & call by reference in PHP. This is the most important concept used in programming and sometime asked in interviews. Don’t worry if you are new to this terminology, you’ll learn this concept in this tutorial.

Posted on 22-09-2016 

Comment:

To comment you must be logged in members.

Files with category

  • SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    SimpleXML is a PHP extension that allows us to easily manipulate and get XML data

    View: 234    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    SimpleXML provides an easy way of getting an element's name, attributes and textual content if you know the XML document's structure or layout. SimpleXML turns an XML document into a data structure you can iterate through like a collection of arrays...

  • How to use the PHP download file script? Class Download PHP

    How to use the PHP download file script? Class Download PHP

    View: 148    Download: 1   Comment: 0

    Category: Php&mySql     Fields: Other

    This is my favorite PHP download script. I’ve used a different more simple method until a client wanted to be able to allow their site visitors to download a large file from a password protected directory. The PHP script works on Apache web servers...

  • Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    Installing OCI8 & PHP 7.2 on RHEL or CentOS 6

    View: 290    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    You’ll need to stop Apache (web server) using the following command. In any Linux commands I show in this article, it presumes you aren’t logged in as the root user, so you will need to use sudo to issue privileged commands.

  • PHP source code: get content of google search

    PHP source code: get content of google search

    View: 241    Download: 2   Comment: 0

    Category: Php&mySql     Fields: Other

    How to get the results of google search and display on your website. This PHP code can do that. Refer to the following code.

  • PHP as a Scripting Language for C#

    PHP as a Scripting Language for C#

    View: 175    Download: 0   Comment: 0

    Category: Php&mySql     Fields: none

    When creating .NET applications (including desktop and web applications), it may be useful to allow extending the application using some scripting language. The users of the application can write simple scripts to configure the application, modify...

  • How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    How to Picking the Brains of Your Customers with Microsoft’s Text Analytics

    View: 4258    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    With the explosion of machine learning services in recent years, it has become easier than ever for developers to create “smart apps”. In this article, I’ll introduce you to Microsoft’s offering for providing machine-learning capabilities to apps.

  • How to MySqli Tutorial PHP MySqli Extension

    How to MySqli Tutorial PHP MySqli Extension

    View: 556    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    PHP provides three api to connect mysql Database.

  • Make Laravel Artisan Commands

    Make Laravel Artisan Commands

    View: 524    Download: 0   Comment: 0

    Category: Php&mySql     Fields: Other

    Artisan is the command line tool used in Laravel framework. It offers a bunch of useful command that can help you develop application quickly. Apart from Artisan available commands, you can create your own custom commands to improve your workflow.

 
File suggestion for you
File top downloads
Codetitle.net - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.net Develope by Vinagon .Ltd