When posting from JS, PHP (5.3.8) returns some line returns in excess. For example:
if (isset($_POST['postId']) && $_POST['postId'] == "test") {
echo 'ok';
}
returns ok↵↵↵
Is this by design? How can I get rid of those line returns?
No, this is not by design. You have three newline output.
Try to delete the close tag of php ?>, if you don't have any html after php code.
Another way is just put exit; right after echo, you won't get newline again.
... right below your
echo 'ok';
paste another snippet:
echo '--------new line------';
if the gap (extra returns) show between your first echo and the -----new line----- it would be more than strange:) However if it shows below the ------new line----- you have probably some extra HTML text (tags) below your PHP statement.
Related
This is an odd issue I ran into that adds a number (so far the number 1) to where the included data goes and positions the included data earlier in the code. I initially thought it might be some code on the TEXT.php that was causing the issue, but I simplified the two files until I got to a point where I have no idea what is causing the issue. The below code should result in the issue, despite how simple it is. While I can work around the issue, I would like to understand why I get a number and the included data moves.
The main code just references the Text.php in three ways.
First way is how I normally include php files.
Second is with some text before the echoed value that includes text, this results in a number appearing where the text should be and the text showing up earlier in the code.
Third is just the echoed value that includes text, this results in a number appearing where the text should be and no text.
All that exists on the Text.php is sample text.
Sample Text
Below is the main code.
<?php
$Value = include 'Text.php';
include 'Text.php';
echo "<br>";
echo "Example: " .$Value;
echo "<br>";
echo $Value;
?>
The main code results in the following when viewed on a browser;
Sample TextSample Text
Example: 1
1
Instead of:
Sample Text
Example: Sample Text
Sample Text
I would appreciate if someone that knows code better than I could explain why this happens this way. Thank you for your time.
The PHP manual page for include says:
Successful includes, unless overridden by the included file, return 1
So that explains where the 1 comes from.
For clarity: include executes the code in the included file. If that happens to involve an echo (or just some plain text outside a PHP block), then it echoes it, right there and then. That explains why you see "Sample Text" twice consecutively.
What include doesn't do is save the output and pass it back to the caller. If you want that then, in general, you should use a function with a return value, rather than an include.
N.B. You can use include with a file which has an inline return statement (see example #5 in the manual page), and it would work in the way you've tried above, capturing the return value. This is the scenario alluded to in the "unless overridden by the file" remark in the quotation above. But that's not what your current Text.php implementation is doing.
The $Value variable is what's returned by php's include function. As per php's documentation, this function returns 1.
Reference: https://www.php.net/manual/en/function.include.php
In the case of your code and output.
<?php
$Value = include 'Text.php'; // This line sets $Value to 1 as the include is successful.
include 'Text.php'; // This line echos the contents of Text.php
echo "<br>";
echo "Example: " .$Value; // Echos $Value, which is set to 1 on line 1
echo "<br>";
echo $Value; // Echos $Value again, which is set to 1 on line 1
?>
Sample TextSample Text <--- The text being included.
Example: 1 <--- The value of $Value is one, from a successful include
1 <--- The value of $Value, from a successful include
to get the output you want
Sample Text
Example: Sample Text
Sample Text
you should type
<?php
$Value = file_get_contents('Text.php');//to get 'Sample Text' in Text.php file
//include 'Text.php';#you don't need to include it, since you get the content of it.
echo $Value."<br>";//Sample Text
echo "Example: " .$Value;//Example: Sample Text
echo "<br>";//Line Break
echo $Value;//Sample Text
?>
I have a following question. I have put a long text (variable type LONGTEXT) into MYSQL database - through command line. Somewhere in this text there's a <br> tag, and near the end of text there's <?php phpinfo(); ?>. If i type SELECT * FROM mytable WHERE id=1, this whole text shows as it is, so it is unaltered (read: both <br> AND <?php phpinfo(); ?> are there. But when I submit query via php like this:
if (mysql_num_rows($result) > 0) {
while($row = mysql_fetch_row($result)) {
echo $row[1];
}
}
Then the text is displayed exactly as I want it on my webpage, that means that <br> tag is processed by the browser as newline, AND <?php phpinfo(); ?> part is IGNORED. That is actually exactly what I want. But my question IS: WHY doesn't <?php phpinfo(); ?> part get processed via browser?? Does the PHP's echo function ignore the <?php tag??
Thanks in advance for explanations.
Because echoing a string is not the same as evaluating it.
PHP generates HTML, which is then processed by the browser. However a string containing PHP code won't be evaluated unless you specifically put it through eval() (hint: DON'T!)
Browsers don't process PHP. It is a server side technology.
Your PHP is reading some text from the database and outputting it to the browser. That the text includes the string <?php is immaterial, it is output from the PHP programme, not part of the script.
When the browser parses it, it just looks like invalid HTML and it tries to perform error recovery (more or less ignoring it as an unrecognised tag).
try this in a new empty browser window:
javascript:document.write('<b>hello <?php ?> is here!</b>');
Then open up firebug/inspector.
In safari, the <?php ?> thing seems to be interpreted as/converted to a comment.
PHP echoes anything you throw to it. If you want to execute the longtext, use eval, if you want to properly display it, you could use the http://php.net/manual/en/function.htmlentities.php function for example.
I am trying to delete a string from a string, but the result of strstr is not finding the string. I will try to be as clear as I can here....
The problem is strpos() is not finding $deletTabHTML. I have alerted it in ajax and it is exactly the same as a line in the commonHTML, but obviously it isn't for some reason I cannot figure out. I am assuming I am missing something 'invisible'? My script works if I hardcode the html to be deleted, so the overall script works.
here is the php:
$commonHTML = file_get_contents($url);
if (!empty($_POST['action']) && $_POST['action'] == 'deleteTab') {
$deletTabHTML = trim($_POST['theHTM']);
if(strpos($commonHTML, $deletTabHTML) !== false) {
$is_deleted="deleted";
}else{
$is_deleted="NOT deleted, ERROR:".$deletTabHTML;
}
echo '{"is_deleted":"' . $is_deleted . '"}';
return;
}
MORE INFO:
jQuery is getting an element from the dom and sending it to a php script which is opening a file and deleting the element:
<li id="contact">Contact</li>
The data returned to ajax is:
<li id="contact">Contact</li>
but for some reason it is not finding it. Thos were copied and pasted from the actual file and a javascript alert. They look exactly the same.
I hope that is enough info.
strpos is case sensitive
Try
stripos()
Aside from attempting to make the text search case insensitive, you also might want to make sure that it contains no unicode characters by using utf8_decode() on it first.
Lastly, it couldn't hurt to do some sanity checks on $_POST['theHTM'] before attempting to use it. (It looks like it's missing an L at the end, but it's also worth using isset() to verify that it actually exists.)
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.
I barely know how to use PHP and I can't seem to make my code show an image if a condition proves true. This is the code:
<?php
$search=get_search_query();
$first=$search[0];
if ($first=="#"){
}
?>
I tried writing this thinking it would work and it didn't:
echo "<html>";
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
Also I tried a code I found which started with the function: header() but it caused a tremendously long error, which said something like header already defined.
Thanks
You have used 'double quotes' incorrectly in the echo statement.
Try the following:
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg' alt='Preview not available' />"
Regards,
Mahendra Liya.
You should var_dump($first) to know what it contains
check if the condition is really getting true
and also put single quote inside the double quote.
if ($first=="#"){
echo 'yes it is true';
echo "<img src='http://chusmix.com/Imagenes/grupos/lujan.jpg'>";
}
close the img tag
The part of the query string starting with # (so-called "hash") is not being sent to the server. That is, if your page is called like myblog.com/foo?bar=baz#quux, you php script will only receive myblog.com/foo?bar=baz. You need javascript if you want to handle urls with hashes.