0

I have a PHP function:

function TicketsDisplay($status, $company = '', $contact = '', $show_status_name = 'no', $StatusColour = '000000', $StatusHeaderFontColour = '#FFFFFF', $FontColour = '#000000' ) {
...
}

that i run here:

<?php TicketsDisplay('Customer Reply',$_GET["seq"]); ?>

i want to add

$order = 'datetime DESC';

as a parameter on the function but how can i make sure when i add to the end it doesn't show errors because the other parameters are blank

UPDATE:

If my function looks like:

 function TicketsDisplay($status, $company = '', $contact = '', $show_status_name = 'no', $StatusColour = '000000', $StatusHeaderFontColour = '#FFFFFF', $FontColour = '#000000', $order = 'datetime DESC' ) {
    ...
    }

and i call my function like:

<?php TicketsDisplay('Customer Reply',$_GET["seq"],'sequence ASC'); ?>

surely it is going to think that the end parameter is $contact ?

deceze
  • 510,633
  • 85
  • 743
  • 889
user2710234
  • 3,177
  • 13
  • 36
  • 54

2 Answers2

6

Unfortunately PHP does not give you the ability to do something like this:

TicketsDisplay('Customer Reply',$_GET["seq"], , , , , $order);

Quite frankly, thank goodness PHP doesn't let developers do this ^^

You actually have to fill in each parameter in between.

A better approach is to use either an object/array when you are passing this many parameters:

So your function could ideally look like this:

function TicketsDisplay($options = array()) {

    // set defaults
    $defaultOptions = array();
    $defaultOptions['status'] = '';
    $defaultOptions['company'] = '';
    $defaultOptions['contact'] = '';
    $defaultOptions['show_status_name'] = 'no';
    $defaultOptions['StatusColour'] = '#000000';
    $defaultOptions['StatusHeaderFontColour'] = '#FFFFFF';
    $defaultOptions['FontColour'] = '#000000';
    $defaultOptions['order'] = '';

    // merge the arrays and have the $options overwrite any $defaultOptions
    $options = array_merge($defaultOptions, $options);

    // use your parameters like this
    echo $options['FontColour']; // will give you #000000 if you didn't pass in something different

    // as suggested by Nathan Dawson use extract to avoid breaking current compatibility within your function
    extract($options);

    // now you can simply:
    echo $FontColour;

}

<?php

// now call it like this
TicketsDisplay(array('status'=>'Customer Reply', 'company'=>$_GET["seq"], 'order'=>'datetime DESC'));

// the code will let you optionally deviate from the defaults of the function

?>

Important

One thing to note is the inconsistent naming convention used in your parameters which will cause you great headaches down the road.

show_status_name vs StatusColour vs company

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • ok, so inside the function how do i call the parameters? – user2710234 Feb 25 '14 at 13:53
  • 1
    This is a good answer but I'd suggest using extract. `extract( $options );` The user can then just do `echo $FontColour;` – Nathan Dawson Feb 25 '14 at 13:56
  • @NathanDawson thank you for the suggestion. I understand the concept but I haven't used it before, what would happen when you pass an array of arrays? – MonkeyZeus Feb 25 '14 at 13:59
  • 1
    If you gave the arrays a key the array key would be the variable name but it doesn't act recursively whereby the child arrays key becomes a variables. E.g. `$options = array( 'option1' => array(1,2,3), 'option2' => array('a','b','c') ); extract($options); echo $option1[0]; // outputs 1` – Nathan Dawson Feb 25 '14 at 14:02
  • ok great - thank you. is it possible to do this to: 'yes', 'StatusColour' => '#000000', 'StatusHeaderFontColour' => '#FFFFFF', 'FontColour' => '#000000', 'order' => 'datetime DESC' ); $DashboardTicketsOptions2 = array_merge($DashboardTicketsOptions, $DashboardTicketsOptions2); ?> 'Customer Reply', $DashboardTicketsOptions2)); ?> 'Needs Action', $DashboardTicketsOptions2)); ?> 'Open', $DashboardTicketsOptions2)); ?> – user2710234 Feb 25 '14 at 14:07
  • It ***is*** possible but you would certainly have to modify the function a little. – MonkeyZeus Feb 25 '14 at 14:11
  • I tried it like this, but it didn't work. any ideas? – user2710234 Feb 25 '14 at 14:12
  • I also assume you meant to write `$DashboardTicketsOptions1` and `$DashboardTicketsOptions2` and `$DashboardTicketsOptions3`? – MonkeyZeus Feb 25 '14 at 14:12
  • what do i need to change in the function ? – user2710234 Feb 25 '14 at 14:13
  • Basically `function TicketsDisplay($options = array()) {` needs to become `function TicketsDisplay($status='', $options = array()) {` and then just remove this line: `$defaultOptions['status'] = '';` and make sure to use `extract($options);` still – MonkeyZeus Feb 25 '14 at 14:15
  • could i not keep the function as you had it above and just use the array ($DashboardTicketsOptions) as all the other parameters when calling the function (just to save the long string in my code when calling the function) – user2710234 Feb 25 '14 at 14:25
  • Yes, that is the initial suggestion from my answer – MonkeyZeus Feb 25 '14 at 14:32
-1

It looks like your function has enough parameters as it is. Instead of looking for a way to add another to the end (which is very simple) I'd suggest using an array instead with defaults and extract to get the values as variables.

To answer your actual question:

TicketsDisplay('Customer Reply',$_GET["seq"], '', 'no', '000000', '#FFFFFF', '#000000', 'enter order here');
Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58