Can someone explain to me when <?= needs to be used or why this programmer would code this way? I'm working on creating a third party module for SPBAS and I nearly figured it out, I just don't know the significance of the two different options I've specified.
Thanks in advance.
<?= functionhere(); ?> is a short hand for <?php echo functionhere(); ?>.
what <?=something?> is the short form of doing <?php echo something; ?>
where as <? something; ?> does whatever something was supposed to do
edit: im generalizing something as any php call, function string, array, object etc..
<?php functionhere(); ?> does not print out the results from the function, <?=functionhere(); ?> does.
This is a shortcut syntax to echo the variable that comes after it. It has the same effect as
<?php echo $variable; ?>
or
<?php echo functionhere(); ?>
in your case.
<?php functionhere(); ?>
will not do anything. unless something is printed out inside the function
For this to work, short_open_tag has to be enabled
<?= functionhere(); ?> = <?php echo functionhere(); ?>
<? functionhere(); ?> = <?php functionhere(); ?>
They are called short tags and can be enabled via the PHP configuration.
They do the same thing. Only difference is <?php is proper syntax.
One is short tag for echo - but it should not be used because if this function is turned off it will output your code. Thanks for the vote down.
Related
What if I want to have HTML code inside my 'if' statement, and then php code in the 'else' part? What do I do? I have tried this, but with no luck..
<?php
session_start();
if ($_SESSION['bnavn']) : ?>
'HTML-code'
<?php else : ?>
'PHP-code'
<?php endif: ?>
I have also tried this, but also with no luck:
<?php
session_start();
if ($_SESSION['bnavn']) : ?>
'HTML-code'
<?php else : ?>
<?php 'PHP-code' ?>
<?php endif: ?>
I have figured it out! I found this link: PHP Session Destroy on Log Out Button
I made the if statement as one of the answers said, and changed my page.html to page.php. I do not know if it have to .php instead of .html, but not it works!
In html.tpl.php I have written :
<?php if($front_page):?>
<?php print 'Google+ '?>
<?php endif; ?>
but its not working moreover its hanging the home page.
Can someone please tell me the correct syntax if it has some error
Should you not be using echo instead of print?
<?php if(drupal_is_front_page()):
echo 'Google+';
endif; ?>
In most themes, you should be overriding page.tpl.php rather than html.tpl.php. You also have a couple of errors in your syntax. The typical code for a template would read:
<?php if($front_page): ?>
Google+
<?php endif; ?>
Note the space in the first line, plus you don't need to print the second.
This is the most random thing I have ever seen, but when I print a variable in page using a simple method like:
<?= $appCnt ?>
<? echo $appCnt ?>
It gets rendered as:
<!--?= $appCnt ?-->
<!--? echo $appCnt ?-->
I have reinstalled PHP and the crazy thing is everywhere else on the page it is working fine. If I echo this before page load it renders fine, but I even have other stuff in page that is rendering properly.
If I do the following, it works but why on earth is that?
<?php echo $appCnt ?>
Thanks for any help.
I believe you need ot enable short open tags
http://php.net/manual/en/ini.core.php#ini.short-open-tag
Have you checked to see that short_open_tag are enabled in PHP? Or, are you using PHP >= 5.4?
I often use HTML template for my application and within the template content there are place holder which I marked as <?php echo $myVar; ?>
Is there a shorter syntax for that? I tried <?php=$myVar?> but didn't work :D
Please give a hint if you know some way. Thank you!
<body>
<?php $myVar = 122; ?>
abb
<div>
<?php echo $myVar; ?>
</div>
<div>
<?php=$myVar?>
</div>
ccc
</body>
<?=$myvar ?>
short_open_tag should be On.
You can use short tags if enabled.
<?= $var; ?>
This is the same as doing this:
<?php echo $var; ?>
To set this up you can find info here, however it's not recommended you use this method.
http://php.net/manual/en/ini.core.php
If ASP tags are enabled you can also do things like this:
<%=$var; %>
<% echo $var; $>
The shorter version is <?=$myVar;?>, but please DON'T do this! :(
Quoting from the comment, in case anyone misses it.
because:
This will not allow much flexibility in moving the servers, i.e. you must control the server or be allowed to change the ini
directives [to turn on short_open_tag], otherwise you are doomed.
It might be deprecated in the future.
Readability is not a trade for functionality. Period.
it is
<?=$myVar; ?>
short tag should be enabled with PHP
If you are going to use inline xml with PHP do not enable short tags.
As documented here
If you want to use PHP in combination with XML, you can disable this
option in order to use inline. Otherwise, you can print it
with PHP, for example: '; ?>. Also,
if disabled, you must use the long form of the PHP open tag ().
You could use double quote echo like this:
<?php
$myVar = 122;
echo "
<body>
abb
<div>
whatever and $myVar displayed here
</div>
<div>
The content is: $myOtherVar
</div>
sometext
</body>
";
?>
But consider using your template with OOP. It would be even cleaner:
Template::header();//Echoes your template header
Template::content($myContentText,$myWhateverVar);//Echoes your template filled with your vars
HTH,
caffein
So, why does this work:
<?php if (condition): ?>
<!--html code here-->
<?php endif; ?>
But not simply this:
<?php
if (condition) { ?>
<!--html code here-->
<?
}
I figured it was just style preference, but I actually can't seem to get it to work the second way. Am I just doing something completely wrong? I can't see the purpose of outputting HTML right in the middle of an if statement if you wanted it to always print.
You need to allow short tag in your php.ini to make <? work
else you have to write <?php } ?>
The second way should work like you described considering you have the closing php tags and < ?php
It does, if you have the right re-opening tag...
<?php
if (condition) { ?>
<!-- html code -->
<?php
}