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

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.

Related

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

Difference between php inside html and vice versa

I was always curious, is there any significant advantage or disadvantage of writing php inside html or vice versa
example:
echo '<ul>'
foreach ($items as $item)
{
echo "<li>$item</li>";
}
echo '</ul>
As opposed to:
<ul>
<? foreach($items as $item): ?>
<li>$item</li>
<? endforeach; ?>
</ul>
Since these essentially generate the same thing, when would you actually use one over the other?
Functionally they are the exact same and won't have an appreciable affect on performance, if any. It comes down to personal preference and readability - if one is clearer than the other and will be easier for others (or the future you) to understand, go with that one.
I personally find it better to use the latter if you actually have PHP in a mostly HTML file. The clear opening/closing tags match up visually with HTML easier. I find it can be hard to line up curly braces visually.
As an example, in the case of a MVC framework I would use the first way of outputting things in a controller or model context, while the second way in my view files. Some templating languages like smarty have similar looking constructs.
ie:
{ if [condition] }
{ /if }
The first one echoes meaningless strings from the IDE's point of view, whereas the latter one is a mix of HTML and PHP and will be handled properly by your editor. In other words, it's better to actually separate HTML from PHP as it allows your editor to parse HTML and provide some usefull features like syntax validation or autoclosing of HTML tags.
basically php is a server side scripting and html is client side scripting. So if it is php inside html then it generates faster response and you can format a better view. However for some scenario you might have to consider the other case for developing.

PHP' if' spanning in different code blocks

Ok, someone has just shown me a piece of PHP code and at the end of the file I've seen a stray <?php } ?> . I thought that should give a compilation error, but it doesn't.
Why is:
<?php
if(1==1){
?>
X
<?php } ?>
valid?
Is it safe to split a statement into multiple php blocks?
PS: I was expecting for something more from the answers then "yes" :D
Yes that is fine, but I would suggest:
<?php if(1==1):?>
X
<?php endif; ?>
It makes it a little more readable then random { and }
From the manual:
Everything outside of a pair of opening and closing tags is ignored by
the PHP parser which allows PHP files to have mixed content. This
allows PHP to be embedded in HTML documents, for example to create
templates.
Welcome to the mysterious world of PHP.
Safe? Yes.
Readable? Not really.
Avoid mixing your PHP logic with your HTML where possible. There are few times when this is a good idea, as it makes reading through and understanding your code difficult.
Yes, this is fine.
It's often useful to drop out of "php mode" for large blocks of HTML - you'll see this technique used anywhere HTML and PHP are mixed.
It is valid, but not recommended if you want to have a code that is maintainable and readable in the long run.
You must bear in mind that every time you "exit" from PHP, you are entering HTML.

why is there different syntax same outcome?

Why is there different syntax same outcome?
For example
# Example 1
if($myCondition == true) :
#my code here
endif;
if($myCondition == true) {
#my code here
}
# Example 2
foreach($films as $film) :
#my code here
endforeach;
foreach($films as $film) {
#my code here
}
Also I have been using <?= for ages now and I now understand that is deprecated and I should be using <?php echo Is this the case and why? It's a lot more annoying to have to write that out each time.
What are your thoughts?
The colon endif, endforeach, etc syntax is known as Alternative Syntax. I can't say for certain why this functionality exists, just that it does and is supported. I can say that I've noticed the alternative syntax used more for templating purposes where it's easy to pick out an endif/endforeach than it is a closing curly-brace in the middle of HTML markup.
The <?= is known as the short open tag. You can probably find all the info you need about its use here Are PHP short tags acceptable to use?
Why should the outcome be different? The one without the brackets is called alternative syntax for control structures and is very useful, e.g. when dealing with HTML.
<?php echo is much more portable because short open tags can be disabled and are disabled by default since PHP 5.3

PHP eval issue with PHP + HTML code

I've got PHP and HTML code stored in a database table. When I get this data, I need to echo the HTML and process the PHP. I thought I could use eval() for this, which works, if I do this eval("echo 'dlsj'; ?> EVALED "); I get "dlsjEVALED" printed out.
The problem is, I get a fatal error when I run longer scripts. Things like:
Parse error: syntax error, unexpected '<' in /home/content.php(18) : eval()'d code on line 1
Best advice - never store php and html code in your database. And avoid eval() like the plague.
I can't really tell what's wrong with your code, as you haven't provided enough information. But even if I did have some advice, I don't think I could give it in good conscience.
You should redesign your whole application so that it doesn't require storing such things in the database. I can't imagine why it would be necessary.
just right der...........
eval('?>' . $content .'<?php');
You need to re-open php mode after the EVALED. Apparently you have to do this with <? rather than the full <?php.
As a rule eval is to be avoided. But rules are made to be broken. There's a thread at When is eval evil in php? that gives some less dogmatic advice.
Depending on what you want to do, it might be suitable to use a template file that you source, with text that will vary stored in a local variable prior to sourcing the template.
As for storing code to be executed in the DB... this does happen in some frameworks like Drupal to provide convenient extensibility, but then Drupal is pretty thoroughly scoured for security weaknesses.
Also if you're writing self-modifying code then you need to use eval(). Not sure if anyone has done that in php but it would certainly be interesting.
I would guess that you're trying to eval() something that contains an opening <?php tag. And that leads to the error at hand.
$contents = htmlentities($contents);
echo html_entity_decode(eval($contents));

Categories