isset fallback not working?

  • Hi,


    I've simplified this post.


    seo_description is empty in the CMS, I'd expect the fallback message to show, but the page is behaving as if seo_description has a value.


    It's showing the Only show if seo_description has content in the CMS message - even though seo_description is empty?


    Code
    1. // Project meta description
    2. if (isset($project_details['0']['seo_description'])) {
    3. echo "Only show if seo_description has content in the CMS";
    4. } else {
    5. echo "This is a fallback";
    6. }


    I've checked the IDs with perch:showall and the ID seo_description is empty.


    Is there any reason why the if statement isn't working?




  • Stephen Meehan

    Changed the title of the thread from “Fallback for perch_page_attributes_extend” to “isset fallback not working?”.
  • Hello Stephen Meehan


    I would use perch_content_custom() to output all the project meta tags:


    PHP
    1. perch_content_custom('Projects', [
    2. 'page' => '/projects/index.php',
    3. 'template' => '_core/_core-double/projects--meta.html',
    4. 'filter' => 'slug',
    5. 'match' => 'eq',
    6. 'value' => perch_get('s'),
    7. 'count' => 1,
    8. ]);


    And you can rely on conditional tags inside the template for the check you're currently performing in PHP.

    ---

    Back to your code. PHP's isset() checks whether a variable has been set (i.e. has a value). If seo_description has some white space (or an empty string) then isset() would return true.

  • Hi hus_hmd


    Thanks for taking the time to respond, appreciated.



    This makes sense:

    Back to your code. PHP's isset() checks whether a variable has been set (i.e. has a value). If seo_description has some white space (or an empty string) then isset() would return true.


    Using var_dump($project_details);, I found seo_description had a value of ["seo_description"]=> string(0) ""


    What confused me was the if statement works the first time, because before any text is added to the CMS seo_description looks like this:


    Code
    1. ["seo_description"]=> NULL


    When seo_description is NULL the if statement works.




    Add the word "demo" to the CMS, check var_dump


    Code
    1. ["seo_description"]=> string(4) "demo"


    Delete the word "demo", I was expecting it to go back to NULL, but it doesn't.


    Code
    1. ["seo_description_test"]=> string(0) ""

    After deleting the text in the CMS, the field is no longer NULL.


    At this point isset is true (thanks for helping me understand hus_hmd).


    At least I now know why it wasn't working as expected.


    ------


    I only noticed this because I was creating a training screencast, and I deleted placeholder text in seo_description. That's when I noticed the if statement wasn't working. In reality, it's unlikely the client will do this.


    However, if it is possible to reset seo_description to NULL after the text is deleted in the CMS, it'd be handy to be able to do that.