Netbeans code format braces in PHP single line statements - php

Intro
Im developing PHP in NetBeans IDE 7.2 (Build 201207171143) and I love the formatting to clean up my code in my custom format.
At the moment I work in a group with colleagues. Some of my colleagues are used to write single line statement without braces (I think this is bad practice).
Examples
What my colleagues do:
<?php
if($stackoverflow == 'Cool')
echo 'Stack Overflow is Cool!';
?>
What I want when I format the code.
<?php
if($stackoverflow == 'Cool') {
echo 'Stack Overflow is Cool!';
}
?>
Question
So what I am looking for is the setting to achieve this. I can find every other setting to have nice braces.
Tools -> Options -> Editor -> Formatting -> PHP
WhatHaveITried
I have found this but I think this isn't the way for what I want to achieve since he talks about macros.
Could someone help me with this?

I am not sure if u have find the solution or not
Tools -> Options -> Editor -> Formatting
Language: PHP
Category: Braces
Select "same line" for all

Agree on a shared coding style! Don't rely on your editor to format and unformat code for you.
As for this example, I would argue that using brackets is better since it's less error prone - but this is something you have to agree upon with your collegues!

Ok so it isn't possible I guess.

It is possible to format braces in PHP.
This is an example:

I finally found the answer to this. It is found under Options -> Editor -> Formatting -> select PHP under the language drop down -> select "Wrapping" category -> scroll down to see "If Statement" and select "Never".

Related

Visual Studio Code PHP Inteliphense Formatting Brace On The Same Line With Statement

I just hate formatting like this:
public function foo()
{
// do the magic
}
I recently installed the PHP Inteliphense extension in VSCode especially for formatting. Exploring the extension settings I couldn't find any option related to formatiing except Format enable/disable.
Is there any possibility to fix this and to get the brace on the first line?
I agree: K&R braces are the norm, and they're a LIFESAVER when it comes to long text extending over many lines. The PSR 12 "coding standards" are just stupid :(
ANYWAY: check out Intellphense issue#509 and issue #730.
Once possible workaround (suggested in issue #509) might be .jsbeautifyrc.
FYI, I use phpfmt for Visual Studio Code. It works great - including brace formatting :)

Automatically convert short array syntax to long (traditional) in PhpStorm?

How to automatically convert short array syntax to long(traditional) in PhpStorm?
I used the feature "Code -> Inspect Code" in PhpStorm and then one by one click convert short syntax to long.
Inspect code
Manually convert
There has to be a way to automate this job? Are there macros in PHPStorm?
go to Settings(Ctrl + Alt + S)-> Editor -> Code Style -> PHP -> Code Conversion -> Array Declaration Style -> tick Force short declaration style Checkbox.
press OK -> press Ctrl + Alt + L -> voila
Bartosz is right. To avoid manual work you can use Short Array Syntax Converter tool with revert (short into long array syntax) option. But be aware, that
Reverting has not yet been thoroughly tested, so use with extreme
percaution!
I'm afraid it's not possible. If it was a failing inspection, there would be a "Fix all problems in file" option in the submenu. Intentions don't have this functionality.
Have You tried setting PHP version to 5.3 and then only search for this via Inspect code? Don't have PhpStorm installed anymore.

PhpStorm how to detect/highlight closing "tag" in PHP's alternate syntax of contol structures

Pretty much what the title says. Is there a way to make PhpStorm highlight the closing "tag" when using PHP's alternate syntax for control structures?
Take a look at this sample code for example:
<?
if($x==5) {
echo "x is equal to 5";
}
?>
If i put the cursor next to or before the opening/closing brace, PhpStorm will automatically highlight the matching opening/closing brace.
Now, if we write the same code but this time using PHP's alternate syntax for control structures, we end up with something like this:
<? if ($x==5): ?>
x is equal to 5
<? endif; ?>
In this case, PhpStorm will not highlight either the opening "if" or the closing "endif;". Is there a way to make it highlight it?
Unfortunately current versions of PhpStorm cannot do that.
https://youtrack.jetbrains.com/issue/WI-14517 -- watch this ticket (star/vote/comment) to be notified on any progress. So far it has not been associated with any specific future version (not currently planned for implementation).
Related: https://youtrack.jetbrains.com/issue/WI-566
The 2017 version of PHPStorm now supports it. But apparently they are having issues with it that they turned off this feature, they did not mention what the issue is though. But you can still enable it by going to: Find Action->Registry->php.brace.alt.syntax
See response here Alternate PHP syntax

pulling and outputing php tags from a database

I am trying to create a dynamic FAQ page. I have the following phtml sample :
<div id="faq">
<!-- Start FAQ "Navigation" -->
<div class="faqBox">
<? foreach($this->aFAQ as $k => $val) : ?>
<?= ($val['mQuestion']); ?>
<?= ($val['mAnswer']); ?>
<? endforeach; ?>
</div>
</div>
Which outputs as follows:
For additional payment options - check or money order, please contact us at iBrandingLevel == 2 ? $this->oStore->getSuppPhone()." Monday to Friday ".$this->oStore->getSuppHoursOpen()." - ".$this->oStore->getSuppHoursClose()." ".$this->oStore->getSuppTimeZone() : "(888) 455-3237 x2 from Monday to Friday 8:00am - 4:30pm MST/Arizona."; ?>
The above text is just the first $val['mAnswer'] (I didnt include the question as that is working properly).
The html is being rendered however obvoiusly the php isn't. the <? and ?> are being removed and just code is displaying. Is there a fix for this? or is my approach fundamentally wrong.
thanks
Your approach is fundamentally wrong, you are outputting PHP code as if it was HTML text and try to execute it.
It is possible to execute code from a string, you can look at the Eval method (http://php.net/manual/fr/function.eval.php) in PHP, but it is not recommended to do this. There are better ways to resolve your specific issues than to output PHP code directly.
What you could do is send a few variables to the view, and use if conditions there.
You could also prepare the full string you need before the view and then all that would be needed is to display it.
To elaborate a little about Eval :
1- If the code you execute within the Eval comes from a user, it is extremely dangerous.
2- If not, there is very often a better solution to the problem, using Eval makes it harder to debug.
Actually, I'm not sure I should answer this.
First, the answer to your request is the mixed eval ( string $code ) php function.
Second, FORGET IT. IMHO, this could be one of the most dangerous things you could think in.
Thanks everybody for the input and resulting discourse. The php code that was being stored in the database was not being input by users, it was all completely internal, however it still shouldn't be there.
I ultimately went through the database and set a %%variablename%% in place of the php code and then upon retrieval I wrote a script that would:
preg_replace("/\%\%variablename\%\%/", $desiredPhpcode, dbRetrievedString).
all instances of %%variablename%%.
It seemed the safer and more sound approach. I don't know if this is an IDEAL approach that anybody else could benefit from if caught in this circumstance or if it 'just works', but I thought I would share.
Thanks Again for the input it helped enormously
PHP is server-side language. Outputting it to client does not make any sense, as there is no one to interpret it.

php syntax with ":", use it or not?

I like to use the syntax with ":" and the "end-stuff".
if($var=='patate'):
echo 'excellent';
else :
...
endif;
I know IDE are used to the {} syntax when it's time to collapse code, but other than that, is there any reason to NOT use this ":" syntax ?
Don't use it. It's ugly and people usually expect the curly-braces syntax they are used to. It's most common when mixing PHP and HTML in the same file (i.e. when using PHP as a template engine) - but you shouldn't do that anyway, at least not if the same file also contains your application logic.
Autoindent scripts will also have trouble indenting your code properly since they usually just know one curly brace = one more/less level of indentation.
However, if you do like : instead of { have a look at python. :)
The alternate syntax is of occasional use if you're in the habit of mixing PHP and HTML blocks, e.g.
<?php if ($something): ?>
<h2>blah blah blah</h2>
<?php endif; ?>
But if you're using a decent syntax-highlighting editor, there's pretty much no point in using it, as the colorization will give you all the contextual hints you need as to what's PHP and what's HTML. And editors like VIM which let you "bounce" on brackets/braces also negate the need - you can't bounce on an 'endif'.
The main reason that people are against using the alternate syntax is because some server configurations may not allow it. If you don't control your server environment or need to share code with others, then it may be best to shy away from using the alternate syntax.

Categories