php explode function

The return value of explode function is obvious in most cases, for example:

$ar=explode("\n","a\nb") return an array that has two elements: $ar[0]='a',$ar[1]='b';

But sometimes the return value may cause confusion. We will give some special php explode function examples below.

what do you think about the value of $ar=explode("\n",null)? Is $ar an empty array, or just a null? In fact ,$ar is an array with only one element and the element $ar[0] is an empty string(i.e. $ar[0]=”, not null) . For the difference between null and empty, please refer to this post.

What about $ar=explode("\n", "a")? It is an array of one element($ar[0]="a").

$ar=explode("\n","\n") is an array of two elements both of which are empty strings($ar[0]='',$ar[1]=''). Likewise, $ar=explode("\n","\n\n") is an array of three elements all of which are empty strings($ar[0]='',$ar[1]='',$ar[2]='').

$ar=explode("\n","a\r\nb") is an array of two elements($ar[0]="a\r",$ar[1]="b").


If you like my content, please consider buying me a coffee. Buy me a coffeeBuy me a coffee Thank you for your support!
Posted in

Leave a Reply