How do I get HTML syntax highlighting inside PHP strings & heredoc syntax? - php

I'm using Sublime Text with the Pastels on Dark theme. My language of choice is PHP. How can I get HTML syntax highlighting inside PHP strings & heredoc syntax?

Name your heredocs after the language you are using. This will syntax highlight in many text editors, including Sublime Text.
For example:
echo <<<HTML
<!-- put HTML here and it will have syntax highlighting -->
HTML;

Wanted to add this as a comment to Ol' Reliable's answer but I am not allowed yet.
Whilst coding outside and then copying in can be a hassle, for people/editors without syntax highlighting in heredoc, an easy workaround is to temporarily add a closing php tag to the heredoc opening tag:
<?php
$myHtmlCode = <<<HTML?>
<h1>I am Highlighted</h1>
<p>Remove the closing php tag above to finish editing</p>
HTML;
?>

Just code outside of php so you can still see the HTML syntax color-coded, and then put that html inside the php once you are done.

Related

How to add highlight to php tags in netbeans (all area between)

In Netbeans I would like to have a background color for all the area between (and including) the php opening and closing tag. From <?php all the way to ?>.
Not only highlighting the tags themself. Is this possible?
In pimcore it would help me to easily distinguish between html-portions and php-portions within view-templates.
Edit:
What i would likt to have is some similar highlighting we have with Smarty for the php code:
http://wiki.netbeans.org/File:SmartySyntax.png

PHP: htmlentities/strip_tags

I've been re-writing my website lately and added a Syntax highlighter so that I can post code snippets. Before, all I did was htmlentities() the string so that it would be safe and not break anything, but now that I have to use a <pre> to highlight code, htmlentites() effectively removes the syntax highlighting from the page. I've been trying to come up with a function that will just perform an htmlentites() on anything between two tags (<entitiesparse> </entitiesparse>) but nothing seems to work. Does anyone know of a function that I can either:
a) Set it to htmlentities() everything but specific tags (like strip_tags())
OR
b) Only htmlentities() things in certain tags (As mentioned above)
You only need to apply htmlentities() to the raw content. So you can apply htmlentities() to the raw content (the article text) and then invoke a function to add syntax highlighting after that. So long as you check that your syntax highlighting code cannot introduce unexpected nasties, you don't need to call htmlentities() again.
And if you're saying that you use the a element to highlight code, I strongly suggest you use the code element instead, which is designed to provide markup for lines or blocks of programming code. The a element should only be used as an anchor for a hyperlink.
For instance, you could use
<code class="highlighted-code">/* line of code here /*</code>
Then you could use a cascading style sheet to provide background colour for any element of type code with class equal to "highlighted-code", for instance:
code.highlighted-code {background-color: yellow}

Get Netbeans to recogzine HTML tags in PHP HEREDOC

I've tried Googling this, but I cannot find any information on it. Is there a way to get Netbeans to recognize the HTML tags inside HEREDOC as HTML tags? For example, if I do:
echo <<<EOHTML
<table>
<tr></tr>
EOHTML;
Netbeans does not complain about this or say that you probably want to close the table tag as it would in the HTML editor. Is there a way to enable this?
This is a documented bug in NetBeans: http://netbeans.org/bugzilla/show_bug.cgi?id=125684

PHP block syntax conventions

Sorry if this a completely nube sounding questioning. I'm new to the PHP syntax conventions, so I'm not entirely sure what I should be looking for.
The book I've got gives the following example as a conventional php block in html code.
<?php
//... some code ...
?>
I get that, but the confusing bit is that the example code I'm looking at some examples from xampp (e.g. the CD collection source code) doesn't seem to follow the same convention.
Instead, the example code reads more like this.
<? include("langsettings.php"); ?>
<?
//... some code ...
?>
Are the two forms just equivalent for all intents and purposes or did I completely miss something crucial to an intro to php here?
Also why doesn't php use closing tags (or does it and have I just not seen them)? I guess I'm thinking of javascript with the closing tags, but I guess either way, they're codebases in and of themselves so it works. It just seems like html has symmetry at the core of it's syntax, but php syntax sort breaks from that symmetry, which is odd.
Thanks for your time.
The only difference between these is that the second requires the setting short_open_tag to be enabled (which is off by default in new PHP version).
<?php regular open tag.
<? Short open tag (disabled by default)
Beyond this, the placement of something like <? include("langsettings.php"); ?> on its own line enclosed in its own pair of <? ?> is really a matter of style specific to the source you found it in. Different projects use very widely different conventions, and PHP books each tend to adopt their own convention.
PHP doesn't unfortunately have any real specific coding conventions such as you might find in languages like Ruby, Java, or Python, which is, in my unsolicited opionion, one of PHP's chief failings as well as one of its greatest flexibilities.
Now, as to whether or not short open tags are good practice for use in a modern PHP application is a separate issue entirely, which has been discussed at great length here.
The two forms are equivalent, but you will find that the shortcode can give you issues. I would stick with the regular tags:
<?php
and the block closed by
?>
Edit: The closing tag is optional, but only if you want everything after the opening tag to be interpreted as PHP until the end of the page.
Anything outside those blocks are interpreted as HTML, so you have to ensure that you watch where you are opening and closing.
For example:
<body>
<h1> The Heading </h1>
<p>
<?php
echo "This is the Content";
?>
</p>
</body>
Will work, and output the php generated string into your paragraph tag.
PHP is similar to javascript in that it doesn't have 'open' and 'close' tags, but rather utilize a semicolon to declare the end of a particular php statement.
include "file1.php";
include "file2.php";
If you forget the semi colon, like so
include "file1.php"
include "file2.php";
That will generate an error.
The closing tag for a PHP block is ?>. The closing tag is not required, but it can be used if you want to interpret part of your page as PHP and other parts as literal HTML. People sometimes do this if they want to do some PHP processing at the beginning of the page, then write an ordinary static HTML page with just a few PHP variables echoed into it.
In other words, text that comes after a <?php tag and before a ?> tag is interpreted as PHP. If the closing tag is omitted, then all text between the opening php tag and the end of the page is interpreted as PHP.
One exception to this is that if you open a conditional statement inside a php block, then close the php block, ALL the following text on the page will be subject to that conditional, until you start a new php block and close the conditional statement. For example, if you run the script:
<?php
if(1==0) {
?>
<B>conditional HTML</B>
<?php
}
?>
the HTML between the two PHP blocks will not appear on the page.
Note that different PHP blocks are all part of the same script. Variables, functions, and classes defined in one block can be used by other blocks on that page, and so forth.
PHP starting tag is <?php and closing tag is ?>.
If there are short tags allowed on server you can use <? ?> syntax also.
You can read more about that on Offcial PHP Documentation
Best regards,
Tom.
Issues regarding short version long open tags have already been covered.
I'll just mention one common gotcha in the question that hasn't been mentioned yet in these answers.
Compare the following:
<?php
/*
* Some comments here, (c) notice, etc.
*/
header("Content-type: text/html");
...
vs
<?php
/*
* Some comments here, (c) notice, etc.
*/
?>
<?php
header("Content-type: text/html");
...
The second one doesn't work.
Why?
There's a blank line of non-PHP code between the first block of PHP and the second. In a server environment that is not using output buffering, the blank line signals to PHP that the headers are all done, and anything from this point on is part of the HTML (or whatever) being sent to the browser.
Then, we try to send a header. Which produces:
Warning: Cannot modify header information - headers already sent
So ... be careful of your blank lines. A blank line INSIDE your PHP is fine. OUTSIDE your PHP, it may have nasty side-effects.

syntax highlighting of multiline php echo statement in notepad++

I'm sure this has been answered before, but i'm tired of searching and don't want to start downloading editors and IDEs without knowing what i'm getting into.
I am just getting started writing php, and i would like to know a way to have notepad++ (or another editor, IDE if necessary) highlight the syntax of a multiline echo statement containing html. I would like to highlight the html inside of the echo.
In Norepad++, NetBeans and other IDEs that I am familiar with there is no way to make the editor highlight html inside echo if your file is recognized by the editor as a PHP file. But you can print html outside of echo statements instead so that html is highlited and use echo only to output php variables inside html. For example:
<?php
$name = 'John';
?>
<b>My name is <?php echo $name;?></b>

Categories