Jump to content

Line break in if query


kaz
 Share

Recommended Posts

I need to implement double quotation marks with a line break in an if query.

my current code:

<script type='application/ld+json'>
    {
        '@context': 'https://schema.org',
        'sameAs' : [
            <?php if ($global->facebook): ?><?= $global->facebook; ?>,<?php endif; ?>
            <?php if ($global->instagram): ?><?= $global->instagram; ?>,<?php endif; ?>
            <?php if ($global->linkedin): ?><?= $global->linkedin; ?>,<?php endif; ?>
            and so on
        ]
    }
</script>

and this is how it should look, if there is an entry:

<script type="application/ld+json">
    {
        "sameAs" : [
            "https://www.facebook.com/company",
            "https://www.instagram.com/company",
            "https://www.linkedin.com/company",
            and so on
        ]
    }
</script>

 

<?php if ($global->facebook): ?>"<?= $global->facebook; ?>",\n<?php endif; ?>
<?php if ($global->google): ?>"<?= $global->google; ?>",<br /><?php endif; ?>

The double quotes work because I used single quotes for the the rest. The line break (\n or br) is not generated in source code.
The code is output as text, maybe because it hangs in the echo? The source code looks as follows:

"https://www.facebook.com/company",<br />                "https://www.instagram.com/company",\n                https://www.

How do I set line breaks correctly, in this combination?

Link to comment
Share on other sites

Even if you enter that manually :

"sameAs" : [
            "https://www.facebook.com/company",
            "https://www.instagram.com/company",
            "https://www.linkedin.com/company"
        ]

the browser still skips the line breaks (as shown in the console).  I guess you can’t override this behaviour.

  • Like 1
Link to comment
Share on other sites

Three possible solutions:

  • Add a whitespace after the closing PHP tag
  • Add an empty line after the closing PHP tag
  • Output a real line break. In your example, you're putting a literal \n in the source, outside of PHP. Use <?= "\n" ?>  instead.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...