Posted in php
1:11 am, May 11, 2021

passing in arrays to your functions rather than using variables

this is probably an old technique, but i discovered it the other day when i was playing around, and i thought wow i should do this to all my functions, i cant see the downside, apart from having to remember the array types that are needed, plus you could add defaults if needed anyway to remember them down the track. 

Anyway here is my example function before adding the array.


$list_all_result = $myclass->list_all(
            $start = 0,
            $max = 50,
            $list_type = "latest",
          $template_file = "list-item.html",
            $search_term = ""

the array is now layed out like the following


   $list_options = [
        "start" => 0,
        "max" => 50,
        "list_type" => "latest",
        "template_file" => "list.html",
        "search_term" => "",

and you just pass the array into the function

So rather than calling the funciton with all the variables in it you can just change the parts of the array 1st and then call the function.


$list_options["list_type"] = "latest";

then run the list_all function with the options


$imports_list_all = $imports->list();

to get the values in the function just loop through the array and assign all to variables


   foreach ($this->list_options_array as $options_name => $options_value) {
    /* ${$options_name} = $options_value; */
      ${$options_name} = $this->db->escapeString($options_value); // escape all incoming data
      $bugs .= "$$options_name: $options_value<br />";

makes your code a bit more complex, but i think its valuable


No Items Found.

Add Comment
Type in a Nick Name here
Other Items in php
rtrim strip white space or strings from the end of a string get the current month as a number show the difference between two dates in years, months, days, hours and seconds counting the occurrence of words in a multidimensional array write string contents to a file with php unable to access the $_FILES when submitting a form php check column exists in table sqlite show all methods or functions in a class (class function version) show all methods (functions) in a class adding an item to an array with php passing in arrays to your functions rather than using variables check server https or http value in php using $_SERVER Remove background image php with image magic get current url with php list a functions variables or arguments php list all the functions in a class php print an array nicely add a toast alert when logging in with half moon dont use md5 for password hashing using password_hash and password_verify php basic page router checking for spam comments function calculate a percentage (dec) difference based on two numbers explode a string into an array using gzdeflate on a string php include tinymce only if a user is logged into the site Quick tool to convert time into decimal generating all css colours with php? php countries array to dropdown form element Fix for Fatal error: Uncaught Error: Call to undefined function file_get_dom() simplehtmldom simple_html_dom Fixing PHP SQLite database is locked warning - Unable to execute statement: database is locked simple usage for php password_hash change string to capital words ucwords change upper case case string to lower case (uppercase, lowercase) convert a string to uppercase with php php substr return part of a string php countries array check if a timestamp is older than one hour making var_dump look nice php mysql table exists function how to display a php file as html with php php convert date now into a sql timestamp Generate a Random User Name return a state name from an australian postcode get the average of a number list using an array in php IP Address PHP Finding the position of a string in another string using strpos PHP str_replace - find and replace part of a string Random string generator function in php suniqid using the disk free space function in php create a mysql or sqlite current timestamp with php
Page Views

This page has been viewed 453 times.

Search Code
Search Code by entering your search text above.

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.


Random Quote
When i was a little kid, I was really scared of the dark. But then I came to understand, dark just means the absence of photons in the visible wavelength -- 400 to 700 nanometers. Then i thought, well, its really silly to be afraid of a lack of photons. Then i wasn't afraid of the dark anymore after that.
Elon Musk