How do I find out the php start flags <? or <?php - php

Is it possible to find out with php if the short start-flag <? does suffice in a script?
is there a ini-variable or do I need to program some function using output-buffering to see the results?

short_open_tag in php.ini turns <? on/off. However, <?=...?> always works in recent PHP versions.
You can retrieve its value using ini_get() - but you cannot change it using ini_set. You can set it using a .htaccess containing php_flag short_open_tag on though.
So you should never use <? for PHP blocks but <?php. For expressions <?= is fine if you don't need to support ancient PHP versions.

Yes, there is an ini variable, and it is called short_open_tags. So simply put ini_get with sort_open_tags as the parameter should return true if short tags is available on the server:
ini_get('short_open_tag')

I did some double check now:
<?php
check_configuration(); // on a blank linux install check for some config flags
function check_configuration(){
$test_short_open_tag=false;
?><? $test_short_open_tag=true; ?><?php
if(!ini_get('short_open_tag') or !$test_short_open_tag){
die( '<br>ERROR: please allow \'short_open_tag\' in php.ini or .htaccess to allow the use of "<?"<br><br>');
}
}

Related

php have some rules with the open tag <? ?>? [duplicate]

This question already has answers here:
How to enable PHP short tags?
(21 answers)
Closed 1 year ago.
I have an application that has some PHP code, the PHP open/close tag I'm using is:
<?
$anything;
echo problem;
?>
The application is not running.
Are there some rules for this type of open/close ( maybe a php.ini config.) that might cause the application not to run ?
P.S. Would the better way be to change all <? to <?php ?
run a phpinfo(), look for the path which php.ini is used. Then open the file and find short_open_tag
Set it to on or off.
But in general.. use <?php ?> this is better.
Use <?php ?>. <? ?> are referred to as short-tags, and not every server is setup to support them.
The <? version of the open tag is called a "short tag." While the php community prefers the use of the full <?php version of this tag, it is still acceptable to use short tags. Ideally, we would all use <?php all the time, but we often deal with legacy code, and changing all occurrences of <? to <?php can be tedious and time consuming. If you find yourself in a situation where you need to allow the php interpreter to recognize the short tag, you may enable it in the php.ini file using the "short_open_tag=1" directive.
For new code you are developing, I would recommend using the long version of the tag <?php, as you know that will be compatible regardless of the ini setting used in the servers php.ini config. For more information on this, see: Are Php Short Tags Acceptable To Use here on StackOverflow.
To figure out why your application is not running, check the webserver error logs, as well as the php error logs, and consider turning up php's error reporting level. This can also be done in the php.ini file using the error_reporting directive. This will allow you to determine what is causing the application to not run, and then you will be more informed for further questions.
You should use <?php ?> to ensure correct parsing.
There are specific times where it is suited or possible to use the short tags like this for PHP
<?=$sign['last_connected']?>
notice the <?= instead of <?php echo

does PHP Version 5.3.2 work without <?php woth with <?

i have one complete website
which was written in php4, now my hosting server is PHP Version 5.3.2, windows 2008 server
and my site is not working, what i found is old site use following syntax
<?
but if i change it into
<?php
page start working. is there any way to solve this issue...
PHP Version 5.3.2 work with
<?
any script which change all
<? to <?php
in all pages.
This is not down to the PHP version, but depends on the short_open_tag php.ini setting.
You can change the ini setting to "1", but the use of short open tags is generally discouraged these days.
Short tags are a discouraged feature of PHP. You should convert all <? to <?php, because as of PHP 6.0, they will be deprecated. (This is partially to better support XML documents, which have a tag that starts with <?)
BTW, you can turn them on in your configuration.
the following code snippet helped me convert all short tags to proper PHP tags, hope it helps you too:
REPLACING SHORT TAGS WITH PROPER PHP TAGS (archived copy)
There is a directive in your php.ini file called "short_open_tag". This is what allows you to use the shorthand or not. They should not be used anymore though, so its better if you update all tags to the new format.
Metropolis

How can I change php.ini by PHP?

I want to do the equivalent to the following line in file php.ini, but from PHP.
short_open_tag = On
Is it possible?
I tried this:
<?php
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'On');
}
$a = 1;
?>
<?=$a;?>
which outputs <?=$a;?>, so it's not working.
Yes, ini_set() is what you want.
An example:
if (!ini_get('short_open_tag')) {
ini_set('short_open_tag', 'on');
}
If you are using PHP 5.3, short_open_tag is no longer an option.
Description of core php.ini directives
Short tags have been deprecated as of PHP 5.3 and may be removed in PHP 6.0.
If you want to change it during a session and forget about it later, use ini_get() and ini_set(). If you want to actually modify php.ini programmatically, you can parse the ini file using parse_ini_file(), change your options and rewrite back to disk. See here for more.
Or you can write your own string replacement routine using the normal opening of a file, preg_replace(), etc.
Although you can use ini_set, be careful (quoted from the PHP documentation):
Not all the available options can be changed using ini_set(). There is a list of all available options in the appendix.
If you are changing options, like magic_quotes and short_open_tags, that's OK. But if you are going to change safe_mode, enable_dl, etc., the function will fail silently.
Many of the options specified above as examples are obsolete/removed security options in former versions of PHP. Consult the documentation if the behavior of ini_set is unexpected (e.g., does not work)
Please edit the php.ini file (just remove the ; and restart your Apache server):
Replace
;short_open_tag = On
with
short_open_tag = On
Now it will work.

PHP arrow operator closing tags

I am writing a php app on the websever I set up at my house. It is a fedora10 machine, running php5 and mysql. I have code like this:
<?php echo $var->function(); ?>
But for some reason the -> is closing the php tag, so the output has 'function(); ?' added to it...is there something I need to change in my php or webserver configuration?
I dont think that you have mod_php enabled in your apache config file, or else you would never see the php code in the output. Here is a good tutorial on setting up php 5 in apache.
Try
<?php echo("foo"); ?>
If that doesn't work, you don't have PHP enabled in Apache.
If your're sure that php is enabled, try this one
<?php
$result = $var -> function();
echo $result;
?>
to debug it a little.. maybe something interesting will raise
Is the php enabled on server? A simple test for determining it:
<?php phpinfo();?>
Put the above line in a .php file and access it.
You could also try this:
<?php phpinfo();
Final closing php tag isn't required.
I ran into a similar problem the other day but I was using
bar ?>
instead of
bar; ?>
It turned out that the short_open_tag option was disabled in my PHP configuration.
I had the same problem with a standard XAMPP installation.
short_open_tag=On
Solved it.

Why is "<?" no longer working and instead only "<?php" works?

I was using xampp to develop locally and then I installed the PHP from the direct installer. Now in some of my PHP code, only PHP code that starts with "<?php" is correctly parsed. Anything that starts with "<?" or "<?=" is completely ignored and just left as is.
How can I adjust the configuration to parse either tokens?
This is a php.ini setting named
short_open_tag = 1 # (enabled)
I recommend you to disable short_open_tag and only work with <?php. When short_open_tag is enabled, it can collide with the XML processing instruction <?xml as both the PHP open tag and XML PI start with a <?.
By using only <? as start preprocessor startup, you can get the preprocessor confused with well formed XML documents. XML stands <? for processing-instruction, imagine an XHTML document with embeded XML that requires XSLT processing... The preprocessor will get confused with the stylesheet processing instruction and will throw an error.
It's higly recomended to use the <?php processor starting tag, try using the
short_open_tag = Off in your php.ini. Also, you can try using <?php ini_set('short_open_tag', 'On'); > if you are getting problems.
You can set short_open_tag = On in the php.ini
It's a configuration option, more information on: http://www.php.net/ini.core (look for short_open_tag).
For the newer version:
short_open_tag = On

Categories