Tag <? Misbehaving in PHP - php

I'm working with a legacy code that someone had left, and it happens to be my task to re-deploy the code. I'm using Windows Server 2003, Apache 2.0.63 and PHP 5.2.10.
It doesn't work. At least, not in the way I had expected it to work. Call it bugs, if you will.
Upon inspecting, I had a suspicion that this code (which appears numerous times in the application) is the culprit.
&$this->
To illustrate the problem, I reproduce this code:
<?php
phpinfo();
//$variable = &$this->request;
?>
The code above executed beautifully and as expected. However, if I change the code to:
<?
phpinfo();
//$variable = &$this->request;
?>
The code misbehaves, and produce this result on the screen instead, which of course, totally unwanted and unexpected.
request; ?>
Now, the code is littered with the similar code as above, and as such, the application now produce output on the screen similar to this one:
request; $user = &$this->user; // This is comment return false; ?>
for a code that reads as:
<?
$request = &$this->request;
$user = &$this->user;
// This is comment
return false;
?>
I had tried to change every <? with <?php whenever &$this-> rears its ugly head, but most of the time, it introduces a new error instead.
I reinstalled PHP and Apache, even using another version of PHP (5.2.6) and it still won't work. I deployed the code in my localhost (Mac OS X, PHP 5.2.8 and Apache 2.0.63) and it worked without a hassle.
Please, anyone, any enlightenment will more than suffice.

In your php.ini, you need to set the following directive:
short_open_tag = On
From the manual:
Tells whether the short form (<? ?>)
of PHP's open tag should be allowed...
If you have time on your hands, you may want to consider replacing all those short tags '<?' with the full-form ones <?php, for better portability (see what just happened to you? :)

my recommendation is not to use open tags, because it can interfere with <?xml codes.
I also had that problem before, just go and replace all <?php to <? , and then again all <? to <?php.
In this way you won't get any
<? <-- one space after the '?'
and
<?php <-- one space after the 'p'
hope this will help...

If you want to use the short tags:
("<?")
they need to be enabled in you php.ini. See this link.

Related

php require_once modify the local variable?

In my web page, I wrote:
<?php
//define('__PUBLIC__', $_SERVER['DOCUMENT_ROOT'].'/public');
$doc_public = $_SERVER['DOCUMENT_ROOT'].'/public';
echo "Before include...<==============>$doc_public";
?>
<?php require_once($doc_public.'/inc/head.php'); ?>
<?php echo "After include...<==============>$doc_public"; ?>
And the page shows:
This firstly happened when I notice the fatal error in the footer, but the head is fine.
Although I can implement define or constant variable to avoid this, I am still curious how it happens.
P.S.: I run this under Apache with a port 8001. This is set in 【apache\conf\extra\httpd-vhosts.conf】. I am running more than one webapp under this site. I just share this information, as I am not sure this has anything to do with this case.
Thanks!
When you require a file, if a variable is modified it affects the original script as well, that's how it's designed. Require doesn't create a secondary environment separated from the including file, it just adds the PHP code in sequence, exactly like if you had written the code in the initial file.
Have a look at the official PHP documentation, the first example is exactly the same as your case
http://php.net/manual/en/function.include.php
(include is the same as require, the latter just throws an error. For more info about differences between include and require http://php.net/manual/en/function.require.php)

Expression engine showing garbage content while using PHP

Expressionengine is showing garbage value when I am using php for Json encode its showing this content {!-- ra:0000000019930c5000007efd6bf7e0f5 --}
here is my code :-
<?php
$entries = array();
{exp:channel:entries channel="sport" category="3536|1830|4102" site="default_site" limit="3" track_views="one" dynamic="no" status="open|featured" disable="categories|category_fields|pagination|member_data" terminate="yes"}
$entries[] = array('title' => '{title}');
{/exp:channel:entries}
header('Content-type: application/json');
echo json_encode($entries);
exit;
?>
If you see this kind of garbage value on the page that means the page has an error.
We mostly find this garbage value on PHP-enabled templates. So if we resolve the PHP errors the garbage will go.
Do not modify the ExpressionEngine core files. If you want to see the PHP errors on the page, turn on the debug mode.
If you remove the exit() function, you will get the output as you want.
The exit() function also exits the execution of ExpressionEngine code that's why you are getting the garbage value.
Even simpler - remove the exit().
As this answer explains, these are annotation tags used for debugging (so you can get a stack trace for nested templates I suppose) and they are parsed out late in the process. So if you exit() it doesn't work. Just make sure that the script ends with no unwanted output and you should be good. I had this problem (in EEv5) and this was the fix.
I've just had the same style of error codes appear, when moving an old EE 2.9.3 site to a Dev server and applying a test domain name.
There were some PHP Includes in the templates, which referenced the live site's server path. When I changed these... all fixed.
For example:
include("/home/sites/domainname.co.uk/public_html/swift/swift_required.php");
...changed to...
include("/home/domain/public_html/swift/swift_required.php");
Yeah ! finally I got the answer its so simple here is the solution :-
go to ExpressionEngine\system\EllisLab\ExpressionEngine\Library\Template\Annotation\Runtime.php
on line no. 65 comment the code return '{!-- ra:'.$key.' --}';

What is the correct way to comment out a PHP variable being echoed using shorthand notation?

I have recently started using the PHP shorthand <?= ?> tags to echo variables etc in my PHP scripts. However I find if I want to then comment out the variable, e.g. <?= //$myVariable; ?> i get a syntax error.
Is it safe to just do this: <?//= $myVariable; ?>
Many Thanks!
The short tag
<?= ... ?>
is translated into
<?php echo ...; ?>
So to comment it out, you have to put something into ... that always shows up as empty. Here's the shortest I can come up with:
<?= false && ... ?>
This works because echo false echoes nothing.
There's no documentation supporting it, so it might be an old compatibility hack, but the following seem to work:
<?//= ... ?>
and
<?/*= ... */?>
Since they're undocumented, I wouldn't depend on them for anything important, but you could use them if you're just temporarily commenting something out while debugging.
So on the question of why <?/*=...*/?> and <?//=...?> work is because there's a PHP feature called short_open_tag, that lets you skip putting php after <? and just go with something like <? echo ...; ?>. It can be disabled in the INI file, and before v5.4 the short hand wouldn't work unless it was enabled. So as long as you're in control of your INI file you should be OK.
BUT, I just checked our 5.6.31 system (it's old yes), but when the short_open_tag is set to false the <? ... ?> get emitted directly to the client as if it were HTML text. So, it may be that you just aren't seeing the text because the browser isn't rendering it.

Unexpected end of file error with valid code

<?php if (arg(1) == 40): ?>
<?php
$block = module_invoke('views', 'block_view', 'home_rotator-block');
print render($block['content']);
?>
<? endif; ?>
For some reason this code is causing causing an unexpected end of file error pointing to the end of the file. I know that this code is valid because it works on my other server. Does anyone have any ideas why this might be throwing the error?
Note: I know it can be formatted differently(without the inside the if statement.
I get the same error when I comment out the $block line and the print line.
Short PHP tags may not be supported on the new server. Instead of using the short tags <? and ?>, use the full ones: <?php and ?>
Or, if you want to enable that on your new server, just change the directive in your php.ini file:
short_open_tag=On
But however, <?php is the official standard and I recommend you use it everywhere so you won't have to change it every time you switch between servers.
You're using short tags for the last line. This could very well not be enabled on your server. Check php.ini to be sure.
Maximus2012 was able to answer the question. I had to to change <? to <?php.
Thanks Maximus2012

PHP files not loading properly

I have a PHP file consisting of the following structure:
<html>... headers, scripts & styling
... some html here
<?php
if($_GET['v'] == 1)
{
?>
... html code here ...
<?php
}
else
{
?>
... html code here ...
<?php
}
?>
</html>
Sometimes the file just loads half, for example if v=1 what would load onto the screen (if I check with View Source also) is something like this: (relative to what I exampled above)
<html>... headers, scripts & styling
... some html here
... html cod
As you can see, the code just cuts off randomly. The is nothing obvious casing this such as a loop or anything. It happens in the middle of HTML code and not inside the <?php ?> tags.
It looks as if the server just decides to stop communicating right there-and-then for no reason. It also happens at a different and random place each time and sometimes loads perfectly fine.
It also only happens on my shared hosting account and not on my localhost.
Is there anything simples that might be causing this?
Did anyone experience this before?
Your code produces a warning (apparently silent) and fails here:
if($_GET['v'] == 1)
if no v parameter was given in the query string.
Do it like this:
if(isset($_GET['v']) && $_GET['v'] == 1)
If you're running an old version of PHP you'll have to make two separate if statements for each of the two conditions.
Make sure you have display_errors turned on.
ini_set('display_errors',1);
Just to make sure there's nothing going horribly wrong.

Categories