Variables and references in PHP

Tram Ho

Hello everyone, surely any of us already know about variables and references when learning the basics of programming when starting the profession. But when working with PHP some people seem to forget that it exists, or some people know it does but don’t know how to use it; Even a few of us have ever used it but have caused some unexpected errors.

To help you better understand variables and references in PHP, and also give me the opportunity to learn more about this feature, this feature, today I will write about the problem of referencing with variables in PHP. .

To ensure the accuracy of terms as well as definitions, in this article I would like to use some archetypal words compared to its definition:

  • Class : Class
  • Property , properties : Properties in the class
  • Object : data types are objects (can be objects or classes)
  • Array : the data type is an array
  • Reference : reference

In this article, I define two functions that help me print out the value more easily:

Reference variables

The variable $b is now called the reference variable of $a , also called (alias), when $b and $a point to the same memory address on the machine. At this point, $a or $b changes value, the other variable will also change.

For example:

Reference variable example

References in objects and arrays

In PHP, object variables are references, while arrays are not. For example:

The result is:

Reference in Object and Array

So how do we want the array to be referenced? I would like to share a useful foreach usage to help us get that:

And here is the result after running the above commands:

Reference in the function

PHP has support for variable reference when entering the function, the syntax:

Here $a is $a reference variable, and $b is a normal variable. Consider the following specific example:

Result:

Reference in function

There are some problems with using reference variables

This is the part I look forward to most in this article. Below I would like to share the errors I have encountered and how to handle them when working with reference variable. Of course there will be a lot of cases missing, hope you can share more to help your article and other friends better understand.

Reference object

To talk about this issue, I would like to give a small problem to illustrate.

  • For class Date with the methods as shown below
  • Given an array of random dates, for example
  • Input a date (in the correct format), output values ​​within 1 year of the date entered.Input example 2021-01-01 . The desired results with the arrays above are 2020-01-02 , 2021-01-01 and 2020-01-05 .

** Below is my recommended code snippet:

Test the code above:

See the results, not as expected, for details, I debug the above code:

Result:

So what is the reason? It is because of that object reference (I mentioned above).

Then is there a way to fix it? Of course yes, I try to use the clone of PHP offline. Here is the code I have slightly modified:

Here are the results:

The results are as expected !! You pay attention when manipulating objects in PHP.

Reference array

I would like to reuse a bit of the example in the reference section in the array, and at the same time edit a bit:

And this is the result:

To explain this problem, it is probably advisable to go deep into the memory of the reference variable, so I would like to see you again next time. Here, I only propose a method to “avoid” it. Please see my rewrite as follows:

Simply replace it with a different variable name, and here are the results:

Conclude

Above is what I have researched, and also gathered from some of my experience with references in PHP, hopefully somewhat useful for you in some cases.

Reference source

This article I mainly refer to on the PHP document page and the rest is due to the practical experience I have.

Share the news now

Source : Viblo