I have some coding which has both PHP & HTML tags... it is a very big coding... now i want to add these coding inside PHP tag... i tried using "echo" statement and adding quotes, concatenation... it doesnt work and showing some error
here is a sample line..
<td>Developer</td><td>:</td><td><?php echo wpws_get_content($url,'.doc-header-link','','user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call')?></td>
the above line has lots of quotes, html and php... i have lots of lines like this...
what is the best way to include (HTML & PHP) inside PHP ??
there's nothing wrong with your line. Follow error you're seeing and try to correct them
http://sandbox.phpcode.eu/g/3a329.php
"adding quotes, concatenation" That's what you need, but it's kind of error-prone to reformat long lines:
echo '<td>Developer</td><td>:</td><td>'
. wpws_get_content($url,'.doc-header-link','','user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call')
. '</td>';
This is just a suggestion so that no matter how long your html codes just a simple include will simply insert it to your php file.
Put you html codes in a different file
Then just include it in your php file.
file.html
<p>Hello World!</p>
file.php
<?php
include("file.html");
?>
Assuming you want to print that chunk of code, you failed to escape the quotes and the special characters ( <, >, & and so on).
Splitted on multiple lines for better clarity.
<?php
echo('<td>Developer</td>');
echo('<td>:</td>');
echo('<td><?php echo wpws_get_content($url, \'.doc-header-link\', \'\', \'user_agent=FairAndroid.com&on_error=error_hide&cache=43289&callback=call\')?></td>');
?>
Related
hello every body i had a problem with echo in php that might be very simple but i'm a bit new in php.
i had a code like this:
<?php echo '<script>var str=Array(1);str.push('.$user_id.')</script>';?>
in this code i want to display script text instead of calling java script. what shall i do?
i have already tried every kind of writing types such as single quotes and double quotes like below
<?php echo '"<script>"var str=Array(1);str.push('.$user_id.')"</script>"';?>
even tried \ at the beginning and end of script but none of them worked.
You need to use htmlspecialchars() function to escape html tags.
<?php echo htmlspecialchars('<script>var str=Array(1);str.push('.$user_id.')</script>');?>
How to use php to output this html code?
The html code is this.
Piano Programs
but I want to use php to show this code
$program_name = "piano_programs";
echo "Piano Programs";
but.....doesn't work, any idea ,thanks
Backslashes to escape the quotes:
echo "Piano Programs";
You need to add slashes before onclick's two double quotes and move your single quote to the right side of .php.
echo "Piano Programs";
I like to curly-bracket my variables so that they stand out in my editor and so that they don't get mixed up with the text that immediately follows.
I have a strange problem with the load function from jQuery. It escapes HTML content that jQuery gets back from the load function. I load HTML output from a PHP file into a div. I use this function:
function XXX(file,divName,functionToCall)
{
$("#" + divName).load(file,null,function()
{
functionToCall();
});
};
The HTML output of the PHP file:
<div onClick="xxx(0,'xxx')" id="xxx"></div>
Jquery converts it into:
<div onClick="xxx(0,\'xxx\')" id="xxx"></div>
Because of this convention I can't use the onClick function, it isn't valid any more. I can't figure out what I 'm doing wrong, does some one know what causes this problem and how to solve this in a good way? I already read other related question on Stack overflow, but I couldn't find an answer how to avoid escaping.
I guess your problem isn't in a PHP block. Here are some rules you need to follow:
For PHP use these rules:
When to escape the char ' :
When you want to use ' in a ' ' block. For example: echo 'test: \' this workes ';
When you don't need to escape the char ' :
When you want to use ' in a “ ” block. For example: echo “test: '
this workes “;
For HTML use these rules:
If you aren’t in a PHP block, then you don't need to escape data. The data you wrote here will directly be outputted. When you want to make a onClick, just use this template: onClick”functionName('stringValue');”
Maybe it's because of the editor:
Some editors will give \' an other color, don't let the colors distract you. It doesn't mean it's correct! Use a file editor with less features ( like notepad ) and open the PHP file where you were talking about. Check again if there are no \'s on places where they shouldn't be.
The problem isn't JQuery in this case. Trust me, look at the PHP file. Did you maybe escaped data outside a PHP block?
This has something to do with you PHP implementation not javascript. When PHP outputs your html it's set to escape quotes.
I manually execute a query and copy paste the output to an editor.Now it is well aligned like:
Hi,
This is sample data.
Thanks.
If I stored the output of the query in a php varibale and print it using echo command it is not aligned. It looks like
Hi, This is sample data. Thanks.
Is it possible to print the output as it is in php?
you need <pre> tag for that. but I suggest you should format it with proper html
echo "<pre>".$youvariable."</pre>";
PHP prints it perfectly fine. If you're looking at it in a browser though, consecutive spaces and newlines are collapsed into a single space by the browser. Replace newlines with <br> tags to keep them, using nl2br.
Seems like a simple question, but I haven't been able to find a solid answer anywhere. I'm outputting a ton of HTML and find escaping "s to be error prone and hard to read, but I also want to have my HTML formatted nicely.
Want something like this (though I know this won't worK):
echo '<div id="test">\n';
echo '\t<div id="test-sub">\n';
echo '\t</div>\n';
echo '</div>\n';
What is one to do?
Thanks.
did you look on HEREDOC
Heredoc text behaves just like a
double-quoted string, without the
double quotes. This means that quotes
in a heredoc do not need to be escaped
example of advantage here : http://www.shat.net/php/notes/heredoc.php
There are a lot of ways to make sure, this works just fine for example (PHP_EOL is a cross Platt form Constant for a new line Char (EndOfLine) ):
echo "<div id=\"test\">".PHP_EOL;
echo "\t<div id=\"test-sub\">".PHP_EOL;
echo "\t</div>".PHP_EOL;
echo "</div>".PHP_EOL;
I make use of a small set of classes I wrote in order to output nicely formatted HTML. If you are interested you can find it here.
To get what you want, I would end up writing something like
$mypage = page::blank();
$mypage->opennode('div', 'id="test"');
$mypage->opennode('div', 'id="test-sub"');
$mypage->closenode(2); // div, div
echo $mypage->build_output_strict();
Another alternative would be to use a full-fledged template engine, of which there are quite a few.
use double quotes
or a multi-line echo string:
echo '<div id="test">
<div id="test-sub">
</div>
</div>';
or templates.