1
Severity: Notice

Message: Undefined variable: canonical

Filename: views/header.php

Line Number: 19


 <? if(isset($canonical) && $canonical) { ?>
    <link rel="canonical" href="<?= $canonical ?>" />
  <? } ?>

Is there setting in php.ini I need to turn on to fix the issue?

icn
  • 17,126
  • 39
  • 105
  • 141

2 Answers2

3

It's better to address the issue rather than suppress the warning message.

The issue is this line:

if(isset($canonical) && $canonical)

The use of isset($canonical) is fine, however you're directly using the same variable in the same scope, which is causing the message.

Change it to this:

<?php if( isset( $canonical ) ): ?>
    <link rel="canonical" href="<?= $canonical ?>" />
<?php endif; ?>

You don't need the extra && $canonical assuming that you don't have code that would ever initialize the $canonical variable to a null or empty state.

Other tips: 1. Use <?php instead of the <? "short tags" because of symbol conflicts with XML declarations. I understand by default newer versions of PHP have short-tags disabled. Note that the <?= ?> shorthand (for echo) is always permitted. 2. Use the if: endif; syntax to make page rendering code easier to follow.

Dai
  • 141,631
  • 28
  • 261
  • 374
  • @Rizier123 That link doesn't have `E_NOTICE` enabled. – Dai May 13 '15 at 21:03
  • 1
    In php if the first logical comparison for `&&` matches as true any future comparisons aren't evaluated. Your assumption of using the same variable withing the same "scope" seems incorrect. – Luceos May 13 '15 at 21:06
  • @Luceos I know the `&&` operator is short-circuiting, but given the voodoo that PHP employs when interpreting and evaluating code it could be raised before then. – Dai May 13 '15 at 23:24
1

First of all let me warn you that you are using short open tag for coding, which is not good practice.Please try this:-

issue is in your condition also and in your link code also,So try this:-

 <?php if(isset($canonical) && !empty($canonical)) { ?>
    echo "<link rel="canonical" href=" .$canonical ." />";
  <?php  } ?>

Note:- Why i tell like above because it's ok that you have enabled setting for short open tag but on other system if they are not enabled it will not work.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98