0

I'm working on a MVC project and i'm on the part to get the URL values, to get each param i use array_shift() and the documentation says this:

Returns the shifted value, or NULL if array is empty or is not an array.

In my code i have these lines:

        $arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : "";

        $controller = array_shift($arrParams);
        $action = array_shift($arrParams);
        $params = array_shift($arrParams);

If i access to mvc-project.local and i don't pass any param to the URL appears this message:

Warning: array_shift() expects parameter 1 to be array, string given in ... on line 12

Where is the problem?

2 Answers 2

4

Try this -

    $arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : array();

Or

    (array) $arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : "";

Or

    $controller = array_shift((array)$arrParams);
    $action = array_shift((array)$arrParams);
    $params = array_shift((array)$arrParams);
Sign up to request clarification or add additional context in comments.

Comments

1

You are defaulting $arrParams to an empty string, so that is why you get a warning (note not an error) about it being passed a string. Just make it an empty array:

$arrParams = isset($_GET["params"]) ? explode("/", $_GET["params"]) : array();

Or a not so good solution is to suppress the warning with @:

$controller = @array_shift($arrParams);
$action = @array_shift($arrParams);
$params = @array_shift($arrParams);

Comments

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.