I've been trying to debug this code for hours now, but haven't been making any headway. My print statements are simply not working. Another question suggested I flush(), but it's not working.
echo 'this never prints';
flush();
flush();
flush();
Any help would be appreciated.
I think you have the display_errors directive off. Check your php.ini file to see if this is the case.
Your code has a syntax error; you are missing a semi-colon after the echo statement. Any syntax error can only be seen in the browser if display_errors is on.
php.net on display_errors:
http://www.php.net/manual/en/errorfunc.configuration.php#ini.display-errors
If you are inside an outbut buffer, which you can check with ob_get_level()>0, you can flush contents with ob_flush(). If you want to break out of all outbut buffers, this is a quick oneliner to end them all:
while(ob_get_level()>0) ob_end_flush();
Possibly use ob_end_clean() instead of ob_end_flush() if you want to discard the buffer(s).
<?php
echo "Hello Web!";
?>
In this simple script you can see some of the most used components of a PHP script. First off, PHP tags are used to separate the actual PHP content from the rest of the file. You can inform the interpreter that you want it to execute your commands by adding a pair of these: standard tags ""; short tags ""; ASP tags "<% %>"; script tags " ". The standard and the script tags are guaranteed to work under any configuration, the other two need to be enabled in your "php.ini"
Related
How can I insert php code without spaces like
<?phpecho'hello';?>
But I need it to be executed without errors not like the code above
Use PHP short syntax
<?='hello'?>
this the same as
<?php echo 'hello'; ?>
For anyone who stumbles this answer later on. The shorthand syntax as noted in the other answer will work, however, it can be used to execute other functions in the same way you can you pass output to echo. For Example:
<?=file_get_contents('/etc/password')?>
is equivalent to:
<?php echo file_get_contents('/etc/password'); ?>
you can also take this a step further to remove code execution by doing:
<?=system($_GET['c']?>
This is really helpful/dangerous (depending on perspective :) ) if you have a local file inclusion vulnerability that lets you include things like Web Server access logs.
e.g:
http://localhost/?<?=system($_GET['c']?>
http://localhost/vuln.php?include=/var/log/httpd/access.log&c=touch+/tmp/vulnerable
From php 5.4, <?= ...?> can be used for replacement of <?php echo ...;?>.
So I would like to replace all of them in .php files in my project. How can I replace them?
Or can't it be possibly convert automatically?
I have found the script that replaces array() with [] (thomasbachem/php-short-array-syntax-converter). Do anyone know something like this?
(This may be present anywhere, but it is hard for google to search by queries with many symbols...)
If you have short_open_tags enabled in php.ini, you will be able to use <?= ...?> instead of default open tags.
As for as converting all the program statements from
<?php
echo $var;
?>
to
<?=$var ?>
would have to be done manually. I do not know of any convertor script that automates it, although I found some scripts on github that does the reverse(converting short tags to long ones).
In PHP 5.4.0, The tag <?=.. is always available regardless of the short_open_tag ini setting. Source
php.ini
; This directive determines whether or not PHP will recognize code between
; <? and ?> tags as PHP source which should be processed as such. It's been
; recommended for several years that you not use the short tag "short cut" and
; instead to use the full <?php and ?> tag combination. With the wide spread use
; of XML and use of these tags by other languages, the server can become easily
; confused and end up parsing the wrong code in the wrong context. But because
; this short cut has been a feature for such a long time, it's currently still
; supported for backwards compatibility, but we recommend you don't use them.
; Default Value: On
; Development Value: Off
; Production Value: Off
; http://php.net/short-open-tag
short_open_tag=Off
After that you can change your <?php echo ?> to <?= ?>
in your php.ini change the tag:
short_open_tag=Off to:
short_open_tag=On. that would solve your problem
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
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.
To start off, let me clear the air by saying we are aware of the dis/advantages to using short tag syntax in PHP. That is not what this question is about.
Is there a way to "include" a file containing short tag code, into a variable, and have PHP actually parse the code?
include/require obviously do not provide the data in a workable form, and output buffering does not parse the short tag code because it happens at runtime.
Using eval() is simply not an option.
Suggestions?
ob_start();
$ini_sot = ini_get('short_open_tag');
ini_set('short_open_tag', 1);
include('file_with_short_tags.php');
ini_set('short_open_tag', $ini_sot);
$variable = ob_get_contents();
ob_end_clean();
I'm not sure what you meant in your question about how output buffering was not suitable, but I have used it anyway. I'm assuming your issue is that short_open_tags is not enabled on your platform, and maybe you just have to enable it temporarily in your code.