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>
Related
Could anyone please help me to resolve this issue shown in the picture ?
The message shown for that line of code means exactly what it says. If what it means by an "escaping function" is not clear, you can search the Web for lots of information about that. It's a fundamental concept in programming, and in website programming in general. You are going to need to understand it to write good, usable, robust webapps / websites.
Another place to start learning about what the message is telling you is noted right in the message: the WordPress Developer Handbook.
When you are going to print any content as output and if you did not properly escaped it then phpcs will generate error and it will say Output should be run through an escaping function. Here i am describing some information how you can escape properly
esc_html() It will be used to escaped any content with html tag. This WILL NOT display HTML content, it is meant for being used inside HTML and will remove your HTML.
<span><?php echo esc_html($title); ?></span>
esc_attr() can be used when need to escape any html tag attributes
<div id="<?php echo esc_attr($variableId);"></div>
wp_kses() When you need to keep any content with html tag then you have to use wp_kses() where you can pass an array of allowed html tag and attributes so that those allowed html tag will not be truncated while printing.
$title = '<span id="testid" class="className">This is a test content</span>'
echo wp_kses($title, ['span' => [ 'id' => []]]);
So if your output need to contain any html tag seems like svg then you can use wp_kses() with allowed html tag so that the require tag would not wipe out with the escaping method and this will fix the phpcs output error issue.
A simple code sample <?echo '<?this text is ignored?> this text is shown';?> writes just "this text is shown" and ignores the text inside php tags despite it's a string.
Unfortunately, I can't find any explanation in specs, so, how to handle this? I know, that we can escape special symbols and everything goes all right, but what's the matter of such php behaviour?
PHP 5.3, local server.
This behaviour prevents from reading lines from php files inside zip via zip_entry_read() and usage of eval() then.
PHP is not ignoring the text inside your inner <? .. ?>, your browser is ignoring it.
Anything you put inside of angle braces is an HTML tag as far as your browser is concerned.
I'm not sure what you were expecting, but if you want the tags to show in the browser you have to replace the opening < with <
If you actually wanted to execute the code inside the inner php tags you can just go ahead and remove the inner tags as they are redundant.
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.
When we use echo in PHP, the code inside the quotes is HTML.
On some HTML editors that I use, they just highlight all of the HTML in one colour, so it makes it a little confusing.
Is there any IDE that gives colouring inside echo?
The only real way you'd get confused echoing HTML is if there was a lot. You could always:
echo {
?>
HTML
<?php
}
Do you mean the colouring for text inside " "? Try PHPStorm (http://www.jetbrains.com/phpstorm/). They offer a 30 day free license to try out the product.
I'm not fully confident I understand what you want, but I believe you're asking if a code editor can also do HTML syntax-highlighting for HTML that exists inside PHP's echo/print statements. Normally not, because most editors see these as strings. However, you can embed PHP into HTML and that usually works a lot better:
<h1><?php print $someVar ?></h1>
If I misunderstood you, and you want syntax-highlighting for the web page...
There are plenty of javascript-based syntax-highlighters out there:
Rainbow
Prism
Sunlight
Try using Notepad++. It comes handy for many of the scripting languages. Here is the download link Notepad++ download link. Its free also. We use that for javascript editing. It works for php also
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