1

I have this in my function:

$str = $_SERVER['REQUEST_URI']; // it gets: /cart/invisible/
$bits = explode('/',$str);
$num = $bits[2];

A problem is that it return mi error:

PHP Notice: Undefined offset: 2 in /public_html/wp-content/themes/my-theme/functions.php on line 3.

P. S. I just checked the information from "a possible duplicate" and there is not exist even any similar example.

How can I fix it?

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
kibus90
  • 315
  • 2
  • 11

3 Answers3

1

The array index starts from 0, so and you are calling $bits[2] which does not exists as your example will return only two elements, try using as $bits[1]

Vidya L
  • 2,289
  • 4
  • 27
  • 42
  • Hello. Thank you for your answer. Unfortunately if I change for '$bits[1]' - I do not get my result. I am using it to get from url word "invisible" - but it can be also different word. – kibus90 Oct 14 '19 at 06:04
1

When you explode $str you get an array indexed from 0 to n as result. When you try to access an index that is not available an error occurs.

$bits = explode('/',$str);
var_dump($bits);

Try var_dump and you can see the array indexes after exploding. Check if it has an index 2.

UPDATE

$num = isset( $bits[2] ) ? $bits[2] : "";
melvin
  • 2,571
  • 1
  • 14
  • 37
  • Hi, thank you for your reply. I got it: `array(4) { [0]=> string(0) "" [1]=> string(6) "cart" [2]=> string(14) "invisible" [3]=> string(0) "" }` – kibus90 Oct 14 '19 at 06:07
  • Hi @melvin - Unfortunately not. I thought I solved it, but unfortunately not... Still "PHP Notice" appears. – kibus90 Oct 14 '19 at 06:30
  • can you share what is in $str and what do you get when you `var_dump( $bits[1] );` – melvin Oct 14 '19 at 06:34
  • I got this `string(6) "cart"` - but I want to get `invisible` – kibus90 Oct 14 '19 at 06:41
  • Then you would surely get `$bit[2]` as `invisible`. To avoid notice or error, just check the update part of my answer. – melvin Oct 14 '19 at 06:45
  • If the value was in element 2 of the array, there should be no notice. Getting an *Undefined offset* message and the value doesn't make sense. – Nigel Ren Oct 14 '19 at 06:52
  • 1
    @NigelRen i just suggested to use `isset` inorder to avoid the notice which is a best practice. – melvin Oct 14 '19 at 06:56
1

If you have var_dumped the $bits variable and got this:

array(4) { [0]=> string(0) "" [1]=> string(6) "cart" [2]=> string(14) "invisible" [3]=> string(0) "" }

as you commented on another answer... Then $bits[2] definitely exists.

The notice must be raised when the code is executed from some other context then you think.

Anyway, you should not just access $bits[2] blindlessly. You should check if it is there first, and if it is not do whatever is appropriate in your use case. And also you should not rely just on bits[2]. The entire path identifies your endpoint. Well not sure what your overall goal is, but you should check that it is invisible only if it is cart and maybe also that there is nothing more after the invisible like /cart/invisible/xyz/ maybe should not be identified as the thing...

slepic
  • 641
  • 4
  • 11
  • Hi, Actually you are right! Because the `$_SERVER['REQUEST_URI']` sometimes returns `/cart/invisibile/?add_it` - that why it could returns the notice. :) Thank you for your answer! Next time I will specify my question! – kibus90 Oct 14 '19 at 07:01
  • @kibus90 I'm afraid that for `/cart/invisibile/?add_it` it would again yield $bits[2] as expected. There must be yet another context... To raise the notice the input string (the request uri) must contain at most one `/` character. – slepic Oct 14 '19 at 07:09