Could somebody explain me why the PHP tags are giving me a linebreak?
And also, how can you delete this or stop this from happening, as it messes up my site.
An example I'm using on my site:
<?php include('assets/common/theme_header.php'); ?>
EDIT:
This doesn't seem to happen when I'm using:
<?php ?>
It does however seem to happen only when I'm using echo, which I also use on my include.
Example:
<?php echo "hello"; ?>
This still gives me a "linebreak", and it shows like this in Chrome development kit:
I had a similar situation where a php file on the server always echoed a space, then newline and then the actual echo. Like " \n[someVariable]". I got rid of it by making sure the php file had no empty lines at the beginning or end of the file.
So no empty line before the <?php or after the end ?>
Perhaps your problem is related.
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
I am fairly new to PHP and trying to create a piece of community learning code for my co-learners to help them and I want to put a coding tip in the comments. I tried writing the following:
# Tip -- when PHP is the only code in a file, don't put ?> at the end as this can cause bugs if white-space or additional PHP code is written to the end of the file after the file is initially created, after the ?>
However, typing ?> in the comment ends the file and the comment. How do I escape this? I tried using \ but that doesn't seem to work, and would also confuse the sense of the comment.
How is this done?
PHP doesn't work well with single lined comments using the closing and starting tags. Although you can use a muti-lined comment:
/* Tip -- when PHP is the only code in a file, don't put ?> at the end as this can cause bugs if white-space or additional PHP code is written to the end of the file after the file is initially created, after the ?> */
Note this is mentioned in the Comment Documentation for PHP:
The "one-line" comment styles only comment to the end of the line or the current block of PHP code, whichever comes first. This means that HTML code after // ... ?> or # ... ?> WILL be printed: ?> breaks out of PHP mode and returns to HTML mode, and // or # cannot influence that.
This works:
<?php echo "\"" . $content . "\""; ?>
This doesn't 500 Internal Server Error:
"<?php echo $content; ?>"
Why? Thank you!
I'm using PHPStorm and MAMP.
Obviously the second will show you error. Because the second one is not even PHP code. It's simply a string. See carefully, the first one you told the server that it's a PHP code which started with <?php and ended with ?>.
But in the second code, you wrote <?php tag inside the quotation. So server doesn't know that it has a PHP code inside. May be you got my point.
I am still struggling with this problem after about 2 weeks with no sign of a solution.
Any data that is returned by PHP using $.ajax or $.post always has two spaces added onto the returned data. I have trimmed the data being echoed in PHP to confirm it's not an issue with the server or my scripts.
e.g.
echo '{"id": "'.$myId.'"}';
Becomes:
' {"id": "'.$myId.'"}'
When viewing the returned data in inspector. This causes problems for my js scripts because they expect nothing returned when there is no error. Double space is returned which causes errors when there are actually none, which in turn stops other events from firing.
I am using Jquery 1.8.3.
Does anyone have any idea what is causing this extremely strange and annoying issue?
I am using NetBeans
I recall that this only started happening since I moved my app to a new server, but I don't see how that would have effected it in this way.
This may be helpful,
I think there is a whitespace in your script (may be from included files, but not sure). You can overcome this by clearing the output buffer, before you send the data to browser. The following code will demonstrate the idea.
<?php
ob_start();
echo ' '; // Possible whitespace (may be from included files)
----------
----------
if (YOUR_CHECK_FOR_AJAX_REQUEST) {
ob_end_clean();
$myId = 1;
echo '{"id": "'.$myId.'"}';
exit;
}
?>
There is chance that you've got whitespaces at the end of your included php files (to prevent it, skip ending ?> tag).
Also you should check if there is nothing in front of <?php and disable BOM in your UTF-8 files.
The data which is returned from the file would have spaces or you would have html tags. Remove all the tags, lines, spaces and at the end remove ?>
If you are returning data from test.php the file should look like as below
<?php
$myId = '1';
echo '{"id": "'.$myId.'"}';
I'm working on making one of my first wordpress themes, but I seem to be encountering a weird issue. Whenever I call one of my functions with PHP, the return (when viewing the page) has a lot of white space (invisible characters). For some of the things I'm trying to do, it causes problems. Here's an example of one of my functions, the rest are built just like it.
// Get YouTube Username
function soc_youtube() {
global $up_options;
?>
<?php if($up_options->soc_youtube){ ?>
<?php echo $up_options->soc_youtube; ?>
<?php
}
}
That code generated this result:
Update: Fixed
Solution: Use less tags and cut down on breaks in code
Everything outside the php tags is pushed through directly to the output, including all your line breaks. To avoid that, leave the line breaks inside the PHP code:
<?php if($up_options->soc_youtube){
?><?php
echo $up_options->soc_youtube;
?><?php //...
(In your example, I don't see the need to close any of the tags at all, though. You could just have everything inside one set of tags.)
First, there is no need to end a php block and start it right back up again with nothing in between... especially on every single line. Try getting rid of those first and see if that makes a difference:
// Get YouTube Username
function soc_youtube() {
global $up_options;
if($up_options->soc_youtube){
echo $up_options->soc_youtube;
}
}
Next, if that doesn't work, try doing var_dump($up_options->soc_youtube); and see what's there and figure out why.