1

I am a beginner to php and i have some trouble with the explode() function.

if (isset($_GET["url"])) {
        $url = $_GET["url"];
        $url = explode("/", $url);
        echo $url;
        $controller = $url[0];
        $action     = $url[1];
    } else {
        $controller = "pages";
        $action     = "home";
    }

I am typing in the url "localhost/pages/home" and the echo is just printing "Array" as if it's empty. If i echo the $url before using the explode function it comes out as "pages/home". Thanks in advance.

1
  • $_GET["url"] would be the value of a query string parameter, such as http://localhost?url=foo/bar. Since you don't have a url= in your localhost/pages/home url, $_GET["url"] is not set and so you always get the else. Commented Apr 11, 2018 at 2:03

3 Answers 3

3

explode() returns the string that you separated as an array(), so PHP's echo doesn't support outputs of array. You must use print_r() or var_dump() functions to do that.

if (isset($_GET["url"])) {
    $url = $_GET["url"];
    $url = explode("/", $url);
    print_r($url);
    $controller = $url[0];
    $action     = $url[1];
} else {
    $controller = "pages";
    $action     = "home";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Not print() for arrays, just print_r() or more favoured IMO as it shows line numbers and data type, var_dump()
2

The problem is likely with your input. The $GET['url'] is not how you access the URL unless you are setting it. You probably want: $_SERVER['PHP_SELF']:

var_dump($_GET["url"]); 
var_dump($_SERVER['HTTP_HOST']); 
var_dump($_SERVER['PHP_SELF']); 

See PHP Server Variables.

Test this: $url = "https://stackoverflow.com/questions/49765341/php-explode-not-working"; $url = explode("/", $url); var_dump($url); die();

Output

C:\wamp64\www\blink\vendor\bausch\yii2-purevision\views\report\index.php:75:
array (size=6)
  0 => string 'https:' (length=6)
  1 => string '' (length=0)
  2 => string 'stackoverflow.com' (length=17)
  3 => string 'questions' (length=9)
  4 => string '49765341' (length=8)
  5 => string 'php-explode-not-working' (length=23)

`

Comments

1

explode() forms an array, comprised of the components that were split up. When you give it the string localhost/pages/home, it forms three components: localhost, pages and home.

Using echo() to output the contents will output the array itself, and just show Array. If you want to output the contents of the array, you either need to use var_dump() or print_r():

<?php

$url = 'localhost/pages/home';
$url = explode("/", $url);
var_dump($url);

Returns:

array(3) {
  [0]=>
  string(9) "localhost"
  [1]=>
  string(5) "pages"
  [2]=>
  string(4) "home"
}

This can aloso be seen at 3v4l.

If you want to grab an individual line, you will need to access it by index with echo $url[index]. For example, echo $url[2] will give you home.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.