executing php script in console - php

i'm trying to execute a php in the console, but each time i run it:
php myscript.php
it only outputs the content of the file, it dowsn't run it.
output:
<?
echo 'test';
?>
instead of:
test
What's wrong? I have php installed under c:/program files/php and the environment variable is set.
Thanks,
Dave

Try
<?php
It might be short_open_tag is disabled in your php.ini
Tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).
Edit: You might also want to read Are Short Open Tags Acceptable To Use?

Try:
<?php
instead of
<?
(if that works you may need to configure your installation of PHP to enable short tags.)

Don't use short tags. Replace <? with <?php.

use <?php instead of <?
edit Try also the -n flags from the cmd line, it avoids PHP read the ini file where short tag could be disabled

Related

Omitting the word php for opening tags

I mean:
<?php echo "hello"; ?>
against:
<? echo "hello"; ?>
(without the php).
It works right, but is there any consequence/bad programming practice?? There is no problem?? Is everything OK? What can happen?
Paraphrased from the PHP manual:
<?php should always work.
<? only works if short_open_tag = On in php.ini.
<?= (equivalent to <?php echo) should always work in PHP 5.4 and newer. In PHP 5.3 and older, it does require short_open_tag = On.
So if you are writing a PHP app for others to use, it is best to stick with <?php (and <?php echo if you decide to support PHP 5.3 or older). If you can control the server's configuration, you can use <? if you want to.
Use, for example, <?php echo '<?xml version="1.0"?>'; ?> if you have to output an XML prolog or processing instruction manually (as opposed to using PHP's XML functions to do it).
It works right, but is there any consequence/bad programming
practice?? there is no problem?? is everything ok? what can it happen?
Well, not every hosting allows short-open-tag. So <?php is a better option if your script target large audience. Or you often change your hosting.
if short_open_tag is open, the short form (<? ?>) will work.
anyway the long form is a standard.

What's the difference between <?php ?> and <? ?>

I can't find the answer anywhere. Thanks!
Basically,
<? ?> are short tags. However, not every php installation has short tags enabled. Therefore, even though is faster to type than the normal tags (<?php ?>), it may not work if you move your code to another server.
Are PHP short tags acceptable to use?
EDIT:
Also, if you're using xml in your web page, you might run into conflicts, as writing <?xml version="1.0"?> will make you run into a PHP error, as xml version="1.0" isn't PHP!
If you're using XML and PHP you may need to <?php echo "<?xml version=\"1.0\""; ?>
In your php.ini, if you want <? and ?> to work, you need to turn on
"short tags".
However, it is better to write long-tag compliant code in the first place.
Acutally is no difference.
They both mean the same, with the difference that the short form <? ?> is not always supported/enabled.
There acutally is no difference between the two, the second one is bascially just a shorthand. I personally would recommend using the longer version, because on some systems, the second possibilty is disabled in the php.ini (see short_open_tags).
It is always better to use <?php ?> as on some installations of php <? ?> is not supported! If this happens your code will not work!

php short_open_tag problem

hi i am working on a project of my college but my college server admin told me that we don’t use short_open_tag in our code what’s its mean? what is short open tag and what is alternate of it
thanks
Normally you write PHP like so: <?php PHP CODE HERE ?>. However if allow_short_tags directive is enabled you're able to use: <? PHP CODE HERE ?>. Also sort tags provides extra syntax: <?= $var ?> which is equal to <?php echo $var ?>.
Short tags might seem cool but they're not. They causes only more problems. Oh... and IIRC they'll be removed from PHP6.
You can try the tool at:
http://blog.eezgu.com/php-short-open-tag-sorunu-ve-cozumu.html
This is a PHP command line tool that converts short tags to normal PHP tags.
The page is not English but you can click the link in the post and download the tool.

Question mark equals, doesn't work on php

this is my php code:
<html><body>Hey!: <?= "World";?></body></html>
It just prints "Hey!:" Whats wrong with my code?
Short tags (which you're using here) can be turned on or off depending on the server you're running the code on. If it's your server, look in php.ini
You need to set short_open_tag to 1
http://php.net/manual/en/ini.core.php
Does your file end in .php and will it execute as php on your webserver? add
<?php echo "yes I run php!<br>\n"; ?>
to your file to be sure. View source to see what happened to the php tags. Then maybe switch on short tags as the other answers told you to.

PHP is displaying my source code?

I just upgraded to php5.3 and when I execute:
php myfile.php
I see the source code for myfile.php.
Any idea why this is happening? I've never seen anything like it before!
In 5.3
short_open_tag
is disabled by default. So if you use
<? ?>
instead of
<?php ?>
it can cause issues in the future (provided you just re enable the flag in php.ini). From what I have read they are still debating removal from 6.
Are you sure you didn't forget the opening PHP tag at the begining of the file ?
ie :
<?php
echo "hello, world!";
(Note I used "full" open tags ; ie "<?php", and not short open tags "<?" -- maybe you used short open tags, and those are disabled ? If I remember correctly, they are disabled by default, in PHP 5.3)
and not :
echo "hello, world!";
(If that doesn't help : can you post a short code sample that reproduces the problem ?)

Categories