php line of code not executing - php

Really puzzled on this, probably simple but it is the day after New Years.
I have one line of php 'inline' code that fails to execute on the server before the server sends the page to the browser.
Here's the code:
<div>
Server says this: <?php "DOOSH"; ?>
</div>
That's all I have in the body of my html test page. I'm expecting the inline php to put 'DOOSH' there into the html then send the html over the client. Not happening.
All I see in the browser is "Server says this:" and nothing else.
The reason this has me confused is -- I KNOW the php executes on the server BEFORE the html above is sent over, so why is the DOOSH not being sent? I have tried 'DOOSH', "DOOSH", I even tried writing a php function in the file to return "DOOSH" and nothing works -- do I have a syntax problem here?
(NOTE: Doesn't have much to do with my problem here, but I can successfully click on the "Server says this =>" link and I see thePhpFile.php successfully load, as expected.)

You need to echo it.
<?php echo "DOOSH"; ?>
Or the shorthand way,
<?="DOOSH";?>

I think the syntax you may be looking for is
<?="DOOSH";?>
Right now, you just have a statement which contains the string "DOOSH" but doesn't do anything with it. You either have to use the syntax above or echo it.
<? echo "DOOSH"; ?>
Or maybe I"m totally misunderstanding...

<?php echo "DOOSH"; ?>
Or
<?= "DOOSH"; ?>

Related

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.

PHP Exits code from =>

I have a chunk of PHP given to me to put into a web page... the code looks like this:
<?php
$SVQuerystring = "";
foreach (($_GET) as $SVGetKey => $SVGetValue) {
....
?>
Problem is, everything after the '>' in the 3rd line gets printed as html text, so the php is being exited there. I can't find a similar issue anywhere, and I have never worked with PHP before. Any suggestions on what is wrong, or where to look? Any help would be greatly appreciated :)
I would seriously question whether the PHP is getting executed in the first place.
Try this code to make sure it is executing without any of your code.
<?php
phpinfo();
?>
If you see a bunch of output pertaining to PHP and your server then there is some other problem. If you don't see any new output, then PHP is not correctly installed on your server.

javascript inside php code

i have place javascript inside php code, like this
<?php if(condition) { ?><script>do some scripting here</script><?php } ?>
it works perfect when i run it in local.
but when i upload it to the server it doesnt run..
the javascript doesnt work.
Can anyone please help.
thanks,
Devan
First, I'm pretty sure you have to do either:
<?php if (condition) { ?> ** script stuff ** <?php } ?>
or
<?php if (condition) : ?> ** script stuff ** <?php endif; ?>
(Your script has no scope on the if statement, although that could be a copy/paste error).
Second, it could be your script (perhaps one of the Javascript files such as jQuery isn't on your server).
Your problem is pretty vague...
Here's all the things I would review first :
are you sure php runs on your distant server ?
do you have anything on screen when running your script ?
when you view the source code from the generated page : do you see your javascript code ?
did you check for javascript error messages ?
Another batch of questions after the comments :)
Did you try to change your JS code with a "basic" one, such as "alert('my code works');" ? (in order to see if your javascript code is triggered)
If the basic alert code does not produce a popup, then you should see your browser's setup with javascript. It may not be enabled
if it works (and you don't have any JS error messages) : try to place some more "alert" popups along your own code, in order to find where your code stops to work)
This will not provide you a direct answer to your problem, but will help you to find more details on your problem, and will help us find an answer

Unbelievable PHP script won't echo out string

I have a test.php script which contains this:
<?php echo 'test'; ?>
When I point to it via my browser, it works and prints out "test" as expected.
I have another script which I am trying to test but however, it is ignoring all my echos! I thought I would put an echo right at the top as this will surely work, but when I get to this script via POST request from a form. It does not even echo out what is at the top of the line:
<?php echo 'START START START';
error_reporting(E_ALL);
I even point to from my browser and still no output?
What the hell is going on?
Update
The error log shows this :
PHP Parse error: syntax error, unexpected T_ECHO in /var/www/tester/convertor.php
But the echo is at the top of the line. I am going to set diaplay errors.
You have a parse error, so the script isn't even executed. So it's expected that it won't output anything.
The parse error can be for any echo in the script. It may be because of an "echo" statement after a line missing a semicolon, for example.
Things to try:
Check your error log
Check the headers (is it coming back 404?)
Make sure you view-source: don't just look in the browser
Delete everything except the echo. If it works, add things back a bit at a time until it breaks.
Load your script in a hex editor and make sure there aren't any weird characters in there
Are you including this file from another file? If so, check the other file too. Watch out for output buffering.
Put exit; after the echo start start start and see if that works. Look in the apache error log for clues. Comment out the rest of the PHP code in the file and see if it works then...
UPDATE
I have had this when copy pasting white space from a browser sometimes - e.g. copy/pasting some code from a web page. Sometimes weird control characters get embedded invisibly in the white space, and I find that deleting the whitespace and re-entering it fixes it. Did you copy paste anything into this file?
Are you hosting this page on a server with PHP installed?
If you just have a .php file on your hard drive somewhere and open it in a web browser, it won't work. You need to be running a web server with PHP extensions and access the file using the HTTP or HTTPS protocols.
On a similar note to this i have seen alot of scripts throw errors like this when they have come from developers on windows (i'm on linux).
I have to go to the start of the php file and hit delete a couple of times to get rid of some invisible characters before the script will run.
You're missing the ending ?>
<?php
echo 'START START START';
error_reporting(E_ALL);
?>

PHP system function strange behavior

I have a strange behavior with PHP system function. In this php script there are only 2 instructions (the rest being pure html):
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
<?php echo system('cgi-bin/gallery2/galleryview.cgi'); ?>
The first cgi just returns a single line as you can check here
http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi
It returns
My Gallery
But the whole php script returns My Gallery Twice:
My Gallery
My Gallery
http://reboltutorial.com/gallery2.php
Is there a reason (I don't use My Gallery in second cgi script of course see http://reboltutorial.com/cgi-bin/gallery2/galleryview.cgi) and how to prevent this ?
Thanks.
Update: The system function will do two things. The first is, it will run a command and pass its output through to the browser and/or output buffer. The second is, it will return the last line of output. So when you're saying
echo system('/...');
You're saying "Hey system, output the results of this command" and then "Hey ehco, output whatever system returns". Removing the echo
system('/...');
will fix your problem.
A few other things to check
Are you sure its galleryheaderview.cgi that's returning things twice? Comment out the include to make sure its actually the script that's echoing My Gallery twice
Is your PHP page/program included/constructed in such a way that galleryheaderview.cgi is being called twice?
Are you sure that calling the URL http://reboltutorial.com/cgi-bin/gallery2/galleryheaderview.cgi is calling the same command line as cgi-bin/gallery2/galleryheaderview.cgi?
If you've checked out the three items above, you'll need to drop into the source of galleryheaderview.cgi and see why its outputting the header twice.
Are you absolutely sure that nothing else is outputting the My Gallery before this line? You should try removing it, and see if it goes completely away or if there is still is one "My Gallery"
<?php echo system('cgi-bin/gallery2/galleryheaderview.cgi'); ?>
If this doesn't bring you any further, maybe you have included some php file twice?

Categories