0

I'm using explode to break a FQDN and store it in an array. Calling pop on that array however returns a blank or empty string, and I can't figure out why.

 $domains = explode(".",str_replace("example.com","","test.example.com"));
 print "Test: " . $domains[0] . "<br>";
 $target = array_pop($domains);
 print "Target: " . $target . "<br>";

Running the above code results in:

 Test: test
 Target: 

Why doesn't $target contain "test"?

1
  • Didn't checked that as you.. But, now its clear, try var_dump($domains) to see.. It contains 2 elements.. test and an empty string. However, this is not a reason for a downvote (imo) Commented Nov 29, 2013 at 22:46

2 Answers 2

2

In effect, here's what you're actually doing:

var_dump(explode('.', 'test.'));

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

You get two elements in the array: "test" and what's after the period i.e. an empty string.

Sign up to request clarification or add additional context in comments.

Comments

1

array_pop() pops and returns the last value of the array

Use array_shift

1 Comment

I actually want the last element of the array, it was the whitespace as mentioned by Nev Stokes that threw me off. Thanks for the reply!

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.