Aptana Studio 3 doesn't recognize PHP inside .html - php

When implementing code like i.e.
<!DOCTYPE html>
<html>
<body>
<?php
echo "My first PHP script!";
?>
</body>
</html>
Next to <?php it gives red X icon with error that "<" missing '>' for end of tag. I did find some older threads about this here at stackoverflow, but there wasn't really any solution or response at all. I tried the solution of adding PHP Source Editor for .html and setting it as default, but looks like it doesn't work.
It's not a deal breaker, it's just I don't like red icons and false errors throughout the code.

This is because .html files are opened with HTML editor, not PHP.
You can try to right-click on your file, then select "Open With" -> "Other ..." -> "PHP Source Editor".

The title of your question suggests that you are saving the files as .html
If this is the case then there will be an error as Aptana will not be looking to validate <?php ?> tags. If I'm right then you will need to save the files as filename.php and not filename.html.
If this is not the case then you might want to change the title slightly.

The tag is legit html. The file he's editing is html. The stock html editor does not flag this as an error. Sorry - I just joined and I can't "comment" yet - but my answer would be...
This looks like a bug in the Aptana HTML editor.

I see this on occasion. If you're editing OPC (Other People's Code), especially WordPress code, sometimes an IF statement will be opened in one PHP file and closed in another. This is often seen in "the Loop" and is the way it's built.

Related

How can I format PHP files with HTML markup in Visual Studio Code?

I'm using Laravel so all the views are .blade.php files. Visual Studio Code won't format the HTML because of the PHP extension. I removed the "blade" part of the filename, but it still isn't formatting the files properly (via Alt+Shift+F).
I also tried about five extensions, but none of them do the reformatting.
How can I format .blade.php files in Visual Studio Code?
The extension Beautify (from HookyQR) just does it very well. Either add PHP, and any other file extension type, to the configuration. As said by Nico, here is an example:
Go to user settings (Ctrl + Shift + P → User settings (UI) or Ctrl + , (comma)
Search for Beautify in the field above. And click on "Edit in settings.json" for "Beautify: Config".
For the "html" section, add "php" and "blade".
###Usage
You can invoke it directly. Press F1, and then write Beautify. The auto completion gives you two choices, "Beautify file" and "Beautify selection". Choose the one you need, and it will do the job. That's a straightforward direct way.
You can also add a keybinding to have a keyboard shortcut. Here is how to do it:
Open keybindings.json (go to menu File → Preferences → Keyboard Shortcuts)
Click in the above. Open and edit file keybindings.json
Add the following into the closed brackets, []
{
"key": "alt+b",
"command": "HookyQR.beautify",
"when": "editorFocus"
}
Choose any key you want, and make sure you don't override an existing one. Search first in the left side if it exists or not.
Note that all of those things are well documented in the description of the extension.
Extra: Note about blade
(suite to #Peter Mortensen clarification pinpoint)
blade or blade.php
If you are confused if it's blade or blade.php for the setting! I'll clear it up for you! It's blade! That's vscode keyword for the language!
How do you know ?
First if you open the list of languages as by the image bellow:
Write blade
You can see Laravel Blade (blade)! The language keyword is in the paratheses! blade!
Well but how to check!
Try with blade.php in the settings!
Try to beautify
You'll get an asking context menu for what language (html, css, js)!
So it doesn't works!
To really know ! Put back blade!
And it will work and beautify directly!
How well it works with blade
The awesome answer to that! Is try it, and see for yourself!
But if you ask me! I'll say it does work as with html! It can be too handy! And you may need to fix some lines! Depending on your expectations and your style!
Here an illustration! I screwed the indentation in purpose
And here the beautification result:
Beautify (from HookyQR) does solve this problem!
Many people add "blade.php" and "php" to the HTML configuration. Beautify does not recognize and manually choose an HTML template. Instead, just adding "blade" to the HTML configuration fixes all issues.
I think Beautify (by HookyQR) might do the trick. Just add 'php' and/or 'blade.php' to the HTML section of its configuration to beautify PHP / Blade view files.
It works for me!
If you want something that just works out of the box, add the format nested HTML in PHP files support Visual Studio Code should already have.
It uses all the native settings for html.format, format on save, etc. Give the extension Format HTML in PHP a try. I made it because every other solution made me annoyed, because it didn't work 100%.
I found this extension called Format HTML in PHP that works well for me.
Andrew Schultz mentioned the extension: "Format HTML in PHP".
This is indeed a great extension for format PHP files with HTML markup. It also formats JavaScript code included in PHP files.
Moreover. Good news! The developer is already working successfully to include Laravel blade.php files. In the meantime I format Laravel blade files in Visual Studio Code by switching language manually from Blade to PHP then I use the extension "Format HTML in PHP" with Ctrl + Alt + F or right click. It works great for me.
Using a newish feature in php, vscode seems to format and highlight embedded HTML beautifully. (php 7.3, released after the question was posted) adds what are called "heredoc" and "neardoc" strings. They are well described here: https://andy-carter.com/blog/what-are-php-heredoc-nowdoc.
Basically, you put your HTML in like this:
[php code.....]
echo
<<<HTML
<div>
<h1>This is a headline</h1>
<p> this is a paragraph. lorem ipsum lorem ipsum blah black </p>
</div>
HTML;
[^ don't forget the ; follow with more php if you want]
it looks just great on screen.
I use Format HTML in PHP which works well in a file containing both HTML and PHP. However it does not appear to format a 'pure' php file such as a Class. So I have also installed Phpfmt, which does a great job with the pure php file but wrecks the indenting in a file with mixed content. Here's an example of what I get:
<?php
if (!$logged_in) {
$login_form = 'data-toggle="modal" data-target="#Modal1"';
$href = '#';
} else {
$login_form = '';
$href = 'members';
}
?>
The opening php tag is correctly aligned with the html. 'I see that under 'Supported Transformations' it says:
AlignPHPCode: Align PHP code within HTML block.
But as you can see from the above, it's not working for me. Any suggestions??
This can be done by using HTML formatting for ".blade.php":
Open Command Palette (Ctrl + Shift + P (on a PC))
Type "Preferences:Open Settings (JSON)" (this is for opening settings.json)
Add
"files.associations": {
"*.blade.php": "html",
"*.tpl": "html"
},
This will format .blade.php files as HTML, but it will not remove Blade snippets.

php open tag was automatically changed with <!--?php

I don't know why. I'm using php7.0.1,apache2.4.18,mysql5.6.28. My php code is like
<?php
echo "hello world";
?>
and I find it turns out to be this in the browser
<!--?php
echo "hello world";
?-->
<html><head></head><body></body></html>
EDIT
I've uploaded the related files to https://github.com/franklee0817/publicFiles
Your server is clearly not parsing PHP code so PHP tags reach the browser. If you see the actual source code (each browser has a different menu item but keyboard short-curt is often Ctrl+U) you'll see your raw PHP source code. However, if you use the DOM tree provided by your browser developer tools you'll the result of parsing and fixing the HTML tag soup. Applying workarounds to invalid tags is left to browser discretion; yours have decided to entirely omit <?php ... > and wrap it into a comment tag.
I find out what's wrong with my server. The php code is not actually running with php. The apache give the php code straight to browser. everything works properly after I add the below line in httpd.conf.
AddHandler application/x-httpd-php .php
The handler for php was missing. It should be as a default line in httpd.conf, I don't know why it was missing. But after this. Everything back on line. Thank you guys.

PHP in Netbeans: Is there shortcut to generate the <?php ?> tags

I've just started using Netbeans (7.1.2, php version) to work on a PHP project. Netbeans is really great for editing long stretches of PHP Code.
But in my view files where HTML is mixed up with short bits of PHP I am getting really tired of manually typing:
<?php ?>
In Dreamweaver you just press a button to create these tags, but in Netbeans I can't find anything like a keyboard shortcut. Surely there must be one. Does anybody know what it is?
In Netbeans I had gone to Tools -> Options -> Editor -> Code Templates and looked through all abbreviations for the PHP language. There were lots and lots of abbreviations for pieces of PHP. But the <?php ?> tags weren't there.
After typing out my question it occurred to me that the place such abbreviations would be was for the HTML language instead of PHP. I looked there and there weren't any code templates at all. But it was simple to add one of my own (abbreviation: 'p', expanded text '<?php ?>').
Now when the cursor's in the middle of some HTML, I type p[TAB] and my tags appear!
Update
Thanks to link from #chris in comment above I can change expanded text to <?php echo ${cursor} ?> and the cursor ends up where I want it.
Looked through the guide and didn't see any
http://netbeans.org/project_downloads/www/shortcuts.pdf
I don't think there is, but you can create a macro to do it yourself :)
You just have to click on Edit -> Start Macro Recording then you have to type in <?php ?> and afterwards click on Edit -> Stop Macro Recording.
There isn't one by default. The closest is typing <? and pressing ctrl+space which will suggest you <?php...
Just type php + TAB to have <?php ?> or phpe + TAB for <?php echo ?>
It's works for Netbeans 8 with default Php Package
If you want, you can change that shortcut on Tools -> Options -> Editor -> Code Templates; choose HTML on Language's select box.

PHP embedding in HTML problem

I'm having problems embedding php inside an html file.
I first ran in to this problem when I was trying to 'include' a php file inside tags, and thought it was related to css formatting, or something. But now I've broken this down into the simplest php and html possible, with an example from a book that should work, and I'm still getting this problem. Here's the html sample that doesn't work:
<HEAD>
<TITLE>PHP inside HTML tester</TITLE>
</HEAD>
<BODY>
<?php
echo "Hello World";
?>
</BODY>
</HTML>
I'm expecting 'Hello World' to show up in my browsers, but nothing is displayed. When I try to 'view source', I see exactly the text above. I figure that after all the examples of this I've tried, the code is ok, but something is keeping what's inside the from being recognized.
Any suggestions? And thanks for helping me out with what's probably a dumb question.
There is something wrong with your PHP installation. The web server isn't passing requests for PHP pages off to the PHP interpreter.
If you did indeed save the file as an .html file, then your PHP code will never execute because most web servers have their handler mappings set to route only PHP (.php, .phtml, or .inc extensions) files to the PHP interpreter.
Looks like your server is not able to handle php or your server does not know how to handle the file type with - this code is in.

Remove blank line on every web page

I have 3 blank lines at the beginning of every page that is causing errors of an add-on with stamps.com. (PS I have worked closely with their support team).
I can see the lines using firebug, but don't know which file is causing them.
I am trying to REMOVE these lines but can't seem to locate them in the header and main_page php files, for templates, common and default folders.
I am using version 1.3.8a of Zen Cart.
The web site is pranajewelry.com
Any suggestions on which files in order of precedence and what special code I should look for?
Ensure all output buffering is of.
Call headers_sent($file,$line); at a point in time you know you already sent output (doesn't matter where, it can be at the end of the request).
Voilà, $file & $line hold the position where output started.
If the return of headers_sent is true, but $line==0, you'll have to look into auto_prepends.
I am surprised that those 3 blank lines cause problems with anything.
Anyway, they could be just about anywhere. Look for any lines in your files that aren't within PHP tags. You could have a common include for example that has 3 blank lines at the end of it. It could be anything. Typically, it will be before or after your <?php ?> blocks.
I'd say those lines would have to be in one of your first includes in your template. Quite possibly you may have whitespace outside of you <?php ?> blocks for your configuration or function include files.
hey if you want to remove blank line then replace that web page/s with "notepad++ software".
it has advance text processing capability. or remove it with "macromedia dreamweaver 8.0"
it has tag replacing options with you can replace blank lines present in all pages in website folder.
Locate this in your files in the directory (use search in files) and you will probably find the blank lines
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en">
Make sure to look after your closing ?> as well. Anything after it will be sent to output; this is actually a good reason why you shouldn't use the closing ?> at the end of your PHP files.
if you are using Dreamweaver try this out
Open the file
Click CTRL + F
Select "Current document" in "Find in" (You can also select the folder if you have multiple files)
Search in "Source code"
Tick "Use regular expression"
Type "[\r\n]{2,}"(without quotes) in "Find"
Type "\n" (without quotes) in "Replace"
Press "Replace All

Categories