0

I have written the following function which returns the value of a key which is the account prefix of a cPanel account. [prefix] = 'oneclick_'

The function works but I'm only a beginner and I'm sure there's an easier and better way to write this function.

<?php
// Get data from cPanel API
$array = $cp_db_restrictions['cpanelresult']['result']['data'];

//Pass array of $data to function
db_prefix( $array );

//Function accepts array of $data
function db_prefix( $array ) {
  if( count( $array ) > 0 ) {
      return $array['prefix'];
  } else {
      return "No prefix";
  }
}
?>

<?php echo db_prefix($array); ?>

The function echos "oneclick_"

EDIT: This is the code which returns the array:

$cp_db_restrictions = $cpanel->uapi(
      'Mysql', 'get_restrictions'
  );

However, I'm not quite sure how to add that into the function. When I try I get the following error message:

Notice: Undefined variable: array in ...

Fatal error: Uncaught Error: Call to a member function uapi() on null in...

SOLUTION*

So, thanks to Nick's answer here is the solution. You must make sure to declare the cPanel class at the top of the document and within the same PHP tags.

<?php
include("/usr/local/cpanel/php/cpanel.php"); // Instantiate the CPANEL object.
$cpanel = new CPANEL();

function db_prefix($cpanel) {
      $cp_db_restrictions = $cpanel->uapi('Mysql', 'get_restrictions');
      $array = $cp_db_restrictions['cpanelresult']['result']['data'];
      return $array['prefix'] ?? 'No prefix';
} ?>
<?php echo db_prefix($cpanel); ?>
1
  • 1
    perhaps use $array['prefix'] ?? 'No prefix' to retrieve the value if it exists, or return a default Commented Nov 8, 2019 at 1:27

1 Answer 1

4

In PHP7 you can just use the null coalescing operator ??:

echo $array['prefix'] ?? 'No prefix';
Sign up to request clarification or add additional context in comments.

5 Comments

is there a way to place the array within the function instead of passing it through an argument?
@HansDesjarlais you could declare it as a global variable (global $array) inside the function, but it is generally considered better practice to pass variables as arguments. Your other alternative might be to include the code which generates the $cp_db_restrictions array inside your function so that it did not need the argument.
that's what I'm having an issue with. Either I'm doing it wrong or the cPanel API call can't be called within a function. Which I think don't makes sense... I edited my question.
@HansDesjarlais yeah, unfortunately you will still run into issues with having to pass variables into the function (in this case it would be $cpanel - although that would be a more reasonable variable to use as a global) but it might be cleaner e.g. 3v4l.org/P9jmb
your bit of code did it. See the edit with the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.