0

Hope someone can help with this

I'm using this to grab the first part of my URL

$page_url = perch_page_url(['include-domain' => false,], true); // 
Output the URL of the current page, minus https
$url_parts = explode("/", $page_url); // Split a string by a string

I'm using this technique to grab the first and second nodes of the URL

$first_node = $url_parts[1]; // First part of string 
$second_node = $url_parts[2]; // Second part of string

On my homepage, there isn't a second node, so I get an undefined offset message.

Is there a way to check if the $second_node exists?

I've tried using

if (isset($second_node)) {
echo "$second_node is set so I will print.";
}

and

if (!empty($second_node)) {
echo '$second_node is either 0, empty, or not set at all';  
}

Both if statements only echo after the $second_node has been set? So I still get the error message.

StephenMeehan
  • 1,083
  • 2
  • 15
  • 26
  • Possible duplicate of [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Loek Jul 09 '18 at 10:44

3 Answers3

1

Please use it directly in the if condition and avoid assigning to a variable or assign after checking it in if condition

if (isset($url_parts[2])) { // remove this line $second_node = $url_parts[2]; 
echo "$second_node is set so I will print.";
}
Praveen
  • 602
  • 4
  • 7
  • Thanks for replying, I can use this in another part of my website. However, for this particular issue I needed to assign the variable in one location and test for it's existence. – StephenMeehan Jul 09 '18 at 12:24
1

That is because the error is thrown at the initialization of your $second_node variable.

Check if the node exists first, and then declare the variable:

$second_node = ""; // Or whatever
if (isset($url_parts[2]) {
    $second_node = $url_parts[2];
}

Check it here: https://3v4l.org/fRJ7L

Loek
  • 4,037
  • 19
  • 35
  • Thanks for taking the time to reply. So I can set the variable, but give it no value. Nice. Also, didn't know about that tool you linked to. Appreciated. – StephenMeehan Jul 09 '18 at 12:28
1

You can do something like this:

$second_node = isset($url_parts[2]) ? $url_parts[2] : FALSE;
if ($second_node)
{
    echo "Second node: {$second_node}";
}

This checks if the third index (0-based) of the $url_parts array is set. If yes, it will assign its value to $second_node. If not, it will assign FALSE so you can handle that further down in the code if you need to check it later (again).

Mario Werner
  • 1,771
  • 14
  • 24
  • This is great, it does exactly what I needed. Thanks Mario. – StephenMeehan Jul 09 '18 at 12:24
  • How would I use your example with this: `$post_title = perch_blog_post_field(perch_get('s'), 'postTitle', true);` – StephenMeehan Jul 09 '18 at 12:30
  • @StephenMeehan I think this is a different question. My example checks if a particular array entry exists. But your example does not use any array. If you want to check if `$post_title` contains anything you can use either `empty($post_title)` or `strlen($post_title)`. – Mario Werner Jul 10 '18 at 15:52