0

Why is this array with empty value even though it has nothing filled?

class URLDynamic {
    private $Parametro;
    private $SepURL;

    private function SetParametro() {
        if ( isset ( $_GET['url'] ) ) {
            $this->Parametro = addslashes ( rtrim ( $_GET['url'] ) );
            $this->SepURL = explode ( "/", $this->Parametro );
        }
    }

    private function SetPages() {
        $this->SetParametro();

        if ( isset ( $this->SepURL[0] ) ) {
            echo $this->SepURL[0];
        } else {
            echo "Home";
        }
    }

var_dump ( $this->SepURL );

array(1) { [0]=> string(0) "" }

It's not falling on the else

mehid
  • 47
  • 6
  • 1
    That is what explode do, either you check for empty value using `empty()` or try [this](https://stackoverflow.com/questions/64570/explode-that-doesnt-return-empty-strings) – catcon Oct 16 '19 at 21:27
  • because php is quite strange language, the person who created it is a good programmer but obviously is a bit psychologically sick, otherwise it is undescribable how he managed to create such an unconvinient language – Dmitry Reutov Oct 16 '19 at 21:29
  • @catcon very good, I just needed `array_filter `before it `explode` – mehid Oct 16 '19 at 21:31

1 Answers1

2

If the delimiter doesn't appear anywhere in the string, explode() always returns an array containing the entire string as its only element, e.g.

explode('/', 'foo') returns ["foo"]

In the case where the input string is empty, this will be an array containing an empty string.

Barmar
  • 741,623
  • 53
  • 500
  • 612