Skip to main content

Don't assign value to reference variable.

Objective:

        I just tried to create linked list in php. 

Challenges:

    In php array is sufficient for most data types (simple array, multi-dimensional array and key-value associative array). But, i just tried using stdClass Object (pre-defined interfaces and class). Here, I created one stdClass with reference key and it reference to another class up to final and that final class  reference key is null. Here I used reference key as reference variable. Once i started assigning reference value to this key it assigning new value to it and old one erased.

$a = new stdClass();
$a->name = "Trial";
$a->ref = null;
for($i=1;$i <= 10;$i++){
 $b = new stdClass();
 $b->name = 'Demo mj '.$i;
 $b->ref = null;
 if($a->ref === null ){
  $a->ref = &$b;
 }else{
  $current = $a->ref;
  while($current->ref !== null){
   $current = $current->ref;
  }
  $current->ref = &$b;
 }
}
print_r($a);
unset($current);
$current = $a->ref;
while($current->ref !== null){
 echo $current->name.PHP_EOL;
 $current = $current->ref;
}

When i run this code it gives below output. It assigned final stdClass to first stdClass reference key.

Technology:

    1. PHP 8.2

Variables in PHP;

    1. Super global variables

    2. Global variable

    3.Function variable

    4. Obscure variable names${'invalid-name'}

    5. Reference variable $a = &$b

    6. Variable variables

$a = "hello"; 

$hello = "world"; 

echo $a." ".$$a; 

 result = hello world 

Reference Variables:

    Here, problem came from reference variable. I thought reference variables are reference that variable but, it shares value commonly and if change any one variable value to change that variable value. So, Once i assign value to that reference variable it assigned new value to that shared value (or change original value).That's why I lost old values. This technic is useful when we want to change original value within function scope.

    Now changed that code and just record it in github repository. https://github.com/Aathiappan/LearningPHP/blob/main/DataStructure/LinkedList.php

Conclusion:

    PHP reference variable not store variable memory address it shares value. It's also useful in function scope. 

    Ref: https://www.php.net/manual/en/language.references.arent.php

Comments

Popular posts from this blog

PHP can run AI models - no need Python

 Objective:     Run AI models only using php. Now this is possible with  https://github.com/CodeWithKyrian/transformers-php  this library. I have tried object detection and question answering using this library. Technology:       1.  PHP 8.2 Initialization:     Guide -  https://transformers.codewithkyrian.com/getting-started 1. Initialize project by composer init cmd. 2. Install transformer library by  composer require codewithkyrian/transformers cmd. 3. Enable ffi extention in php.ini       extension = ffi      ffi.enable = true 4. Enable opcache      opcache.jit = tracing 5. Increase memory limit      memory_limit = 512M I. Object detection: ( https://transformers.codewithkyrian.com/object-detection )     Object detection can improve web application perspective . Example:  Retail applications can speed up checkout by automatically recognizing ...

From C to Assembly: Blinking an LED the Hard Way

  From C to Assembly: Blinking an LED the Hard Way ~4 hours of digging, one blinking LED, and a much better mental model of what "compiling" actually means. The Inspiration I've been programming since 2017 — variables, control flow, HTTP, web apps, the usual path. But I kept coming back to something Linus Torvalds said: that the compiler writes 100% of the code. I wanted to actually understand what that meant before taking it for granted. So I went looking for the layer beneath the code I usually write: binary → hexadecimal opcodes → assembly → C. I wanted to see the whole stack, not just use the top of it. What I Tried I already knew my way around the Arduino IDE for uploading compiled sketches. While researching, I found out you could write raw hex machine code directly for the Arduino — but the resources I found were too dense to follow. So I stepped back one level, to assembly. Searching further, I found a YouTube channel with an Arduino-assembly series. I copi...

IVR or SMS based navigation for smart phone not education equipped people

Problem statement:    On Thursday morning (16-10-2025) i went for walking near Perungudi in chennai. There, i was saw a truck driver he notes the address and has some navigation landmarks. But, he hesitate to follow the navigation due to every day development of chennai. He asked me if you know this location. I said , i don't know but, you may follow the map. He said I have dial pad phone no map in it. So, I was traveled with the track to reach the place used by google map. Then , I realised many Indian old people causing this problem and some poor internet place also. Solution:    If some one know where they are or say some landmark through mobile like 198 (AI). It should say their location on voice call and if they want navigation to some places it should create session with say the navigation tips. It will recall them to ensure they are going correct path. Predictable problems:    1. Voice call or sms not encrypted it may tab by middle one to give wrong ...