Welcome Guest, Not a member yet? Register   Sign In
In views use <?php echo $title; ?> or <?=$title?> ?
#1

[eluser]pierrehs[/eluser]
In views use <?php echo $title ;?> or <?=$title?>
Many people say that we should not use short extensions
Can you tell me if it is better that I use <?php echo $title ;?> or <?=$title?>
which one to choose?
Thank you
#2

[eluser]EugeneS[/eluser]
1) better for compatibility <?php echo $title; ?>
2) better in use and code reading and looks more accurate in views: <?=$title;?>

never had any problems with 2) so, i'm using 2)
#3

[eluser]juanvillegas[/eluser]
My approach is don't use <?= ?>, just because you can't use it for all your php sentences. So, other developers reading your code will have to handle it both ways. It's a minor thing, but if you use three versions of if, three of while and also <?php and <?=, then your code will get messy for sure.
#4

[eluser]EugeneS[/eluser]
[quote author="juanvillegas" date="1284332599"] just because you can't use it for all your php sentences.[/quote]

what do you mean by "all php sentences" ? Smile

this construction used just to show some data and this construction is part of the "PHP’s alternative syntax".

this alternative syntax allows views look like template but not like another controller or php of 90's with the mix of php and html

up to me it's good rule to not use in views objects etc and have deals only with the variables and arrays and use alternative syntax like:

Code:
<?=$variable_name;?>

<? foreach($some_array as $key => $value): ?>
   <div>&lt;?=$key;?&gt;</div>
&lt;? endforeach; ?&gt;

&lt;? if(isset($something)): ?&gt;
   <div>&lt;?=$some_variable;?&gt;</div>
&lt;? endif; ?&gt;

this way for sure looks better in views than ugly mix of php and html
Code:
&lt;?php
   if(isset($something))
   {
       echo '<div>'.$some_variable.'</div>';
   }
?&gt;

or even more ugly way

Code:
&lt;?php
   if(isset($something))
   {
?&gt;
    <div>&lt;?php echo $some_variable; ?&gt;</div>
&lt;?php
   }
?&gt;

complete mess Smile

etc Smile
#5

[eluser]pierrehs[/eluser]
I could use &lt;?= ?&gt; Only for views and have a better readability and for the rest of the files to use &lt;?php ?&gt;
#6

[eluser]EugeneS[/eluser]
in the rest of the files you dont need to show anything
#7

[eluser]Prensil Technologies Pvt Ltd.[/eluser]
It is advisable to use first method instead of the second one as it might create problem with the php.ini setting and we forgot to check. One my project has got the same problem.
#8

[eluser]jmadsen[/eluser]
you figure it out pretty quickly if you've forgotten :-)


We use (and I like) &lt;?= ?&gt; and EugeneS's sample style where ever possible in views. It really does look cleaner and easier to read on a long view file.
#9

[eluser]Georgi Budinov[/eluser]
As juanvillegas already said having differnt php styles in one file/project - brings mess to the code.

This looks very nice
Code:
&lt;?=isset($alabala)?'<div>'.$alabala.'</div>':''?&gt;

but when you have a more complex statement this is getting ugly. So you have to mix statement syntax.
On the other hand I try not to write html tags in php strings e.g. '<div>'.$alabala ...

So I generally prefer the one that perhaps most of you do not like:

Code:
&lt;?php
if(isset($alabala))
{
?&gt;
   <div>&lt;?php echo $alabala; ?&gt;</div>
&lt;?php } ?&gt;

Note here that I do NOT write
Code:
&lt;?php if(isset($alabala)) { ?&gt;
because it is easy to miss that '{' or erase by mistake.
#10

[eluser]Derek Jones[/eluser]
Also CodeIgniter can rewrite short tags for you with the flip of a config variable until you can get your environment sorted out. My personal take is that Views should be simple, shouldn't contain complex logic, and should be legible not to developers but to designers. So that puts me squarely in the alternative syntax camp.




Theme © iAndrew 2016 - Forum software by © MyBB