Jump to content

Pass by reference vs value (preference in PHP)


Recommended Posts

Posted

Just another general coding question. I've been mucking about with arrays today (made much more enjoyable with movie references...) along with various other things I tried to bake in for added learning. I wanted to ask about what the 'norm' is in PHP regarding modifying an original thing (by reference) or returning something new and working with that (by value). The code is this (just looking at the function and usage immediately after it):

Version 1

<?php

$json = '{

  "Bill": {
    "first_name": "Bill",
    "last_name": "Preston",
    "age": 18,
    "location": "San Dimas" 
  },

  "Ted": {
    "first_name": "Ted",
    "last_name": "Logan",
    "age": 19,
    "location": "San Dimas" 
  },

  "Rufus": {
    "first_name": "Rufus",
    "last_name": null,
    "age": 52,
    "location": "Futuristic City" 
  }

}';

// return array
$people = json_decode($json, true);

// by reference
function changeKey($currentKey, $newKey, &$arr) {
  if (array_key_exists($currentKey, $arr)) {
    $arr[$newKey] = $arr[$currentKey];
    unset($arr[$currentKey]);
  }
  else {
    // error rather than returning original array
    throw new Exception("Key does not exist");
  }
}

changeKey("Ted", "Ted Theodore", $people);

foreach($people as $person => $arr) {

  echo "<h1>" . $person . "</h1>";

  echo "<ul>";
  foreach($arr as $key => $value) {

    $key = ucfirst(str_replace("_", " ", $key));

    if (isset($value)) {
      echo "<li>" . $key . ": " . $value . "</li>";
    }
  
  }
  echo "</ul>";
}

Version 2

<?php

$json = '{

  "Bill": {
    "first_name": "Bill",
    "last_name": "Preston",
    "age": 18,
    "location": "San Dimas" 
  },

  "Ted": {
    "first_name": "Ted",
    "last_name": "Logan",
    "age": 19,
    "location": "San Dimas" 
  },

  "Rufus": {
    "first_name": "Rufus",
    "last_name": null,
    "age": 52,
    "location": "Futuristic City" 
  }

}';

// return array
$people = json_decode($json, true);

// by value
function changeKey($currentKey, $newKey, $arr) {
  if (array_key_exists($currentKey, $arr)) {
    $arr[$newKey] = $arr[$currentKey];
    unset($arr[$currentKey]);
    return $arr;
  }
  else {
    // error rather than returning original array
    throw new Exception("Key does not exist");
  }
}

// assign modified array
$people = changeKey("Ted", "Ted Theodore", $people);

foreach($people as $person => $arr) {

  echo "<h1>" . $person . "</h1>";

  echo "<ul>";
  foreach($arr as $key => $value) {

    $key = ucfirst(str_replace("_", " ", $key));

    if (isset($value)) {
      echo "<li>" . $key . ": " . $value . "</li>";
    }
  
  }
  echo "</ul>";
}

Both cases output:

Bill
 - First name: Bill
 - Last name: Preston
 - Age: 18
 - Location: San Dimas

Rufus
 - First name: Rufus
 - Age: 52
 - Location: Futuristic City

Ted Theodore
 - First name: Ted
 - Last name: Logan
 - Age: 19
 - Location: San Dimas

So is there a preference? Thanks.

  • Like 1
Posted

Thanks @FrancisChung I'll read that later this weekend (after the rugby of course!).

I'm working on OOP php at the moment. Been creating classes and objects (using public/abstract/interface), extending them, implementing them, redefining functions, using constructors, working out the differences between public/private/protected/final properties/methods. Having a blast! :) I'll be off the bench and in the game one day!

This does mean my MySQL chapter has been put on the back burner for now though... was getting bored with it tbh, just working in mysql workbench, create, alter, insert, delete, repeat, repeat again. It's important though so I'll go back to that soon enough.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...