dir.by  
  Search  
Programming, development, testing
PHP (язык программирования для быстрого написания Web сайта)
Passing parameters by value and by reference to a function in PHP
  Looked at 5018 times    
 Passing parameters to a function in PHP 
last updated: 5 December 2024
1) Pass parameters by value to a function
Any parameter that is passed to the function does not change the value when the function exits.
That is, when we pass a parameter to a function, a copy of this parameter is created and inside the function we work with a copy of the parameter.
Example
  PHP  
<?php

     // declare a function
     function SetValue($x)
     {
          $x = 10;
     }

     // create a variable
     $a = 20;

     // call the function
     SetValue($a);

     print($a); // we will see on the screen 20
?>
2) Pass parameters by reference to the function
To pass a parameter to a function by reference, you need to use &
When you pass a parameter to a function by reference, such a parameter is passed inside the function and we work with this parameter.
When you exit the function, the parameter changes the value
Example
  PHP  
<?php

     // declare a function
     function SetValue(& $x)
     {
          $x = 10;
     }

     // create a variable
     $a = 20;

     // call the function
     SetValue($a);

     print($a); // we will see on the screen 10
?>
 
← Previous topic
Function in PHP. Example: function CalculateSum($value1, $value2) { ... }
 
Next topic →
Pass a function as a parameter to a function | PHP
 
Your feedback ... Comments ...
   
Your Name
Your comment (www links can only be added by a logged-in user)

  Объявления  
  Объявления  
 
PHP Study
Что такое PHP ?
Function
Function in PHP. Example: function CalculateSum($value1, $value2) { ... }
Passing parameters by value and by reference to a function in PHP
Pass a function as a parameter to a function | PHP
Unnamed function
Anonymous function (no name). Function pointer in PHP
Pass the unnamed function as a parameter to the | using use (...) the unnamed function sees the variable above | PHP
Arrays
Array in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
Look for an element in an array in PHP | array_search
Key-value arrays
Array (key-value) in PHP (Create an array, add elements, the length of the array, go through all the elements in the array)
Regular expressions
Регулярные выражения
PHP Server
Create an html file and javascript sends a post request to the php server. PHP server makes the answer | Why on php server $_REQUEST or $_POST array is empty
Framworks (frameworks)
Zend Framework (PHP framework)
Yii (PHP framework)
CakePHP (framework)

  Ваши вопросы присылайте по почте: info@dir.by  
Яндекс.Метрика