I think it is sometimes easier to use php tags instead of echo for example
<?
if()
echo "<img src='' onclick='alert(\"hello\")'/>";
?>
instead of that I code like this
<?
if(){
?>
<img src='' onclick='alert("hello")'/>
<?}
?>
We got rid of backslashing. But what about strings I want something like this:
<?
$str="?>
<img src='' onclick='alert("hello")'/>
<?";
?>
You should use the PHP heredoc syntax:
<?php
$str = <<<IMGTAG
<img src="" onclick="alert('hello')"/>
IMGTAG;
echo $str;
?>
Enjoy your code.
There is an alternative Syntax specifically for this kind of formation:
<?php if (x): ?>
<div>...</div>
<?php endif; ?>
Also there are short tags:
<?= "hello world" ?>
This directly prints a string and is equal to:
<?php echo "hello world" ?>
For string assignment you can do what Magicianred sugested. You could also do it with output buffering:
<?php ob_start(); ?>
<div>test</div>
<?php
$str = ob_get_contents();
ob_end_clean();
echo $str;
?>
Though output buffering shouldn't be abused for this. Heredoc syntax is the best solution here.
Related
I have delared variables like this. I have created more than 100 variables in every page.
<?php echo $rsedit[customer_name]; ?>
<?php echo $rsedit[address]; ?>
<?php echo $rsedit[city]; ?>
<?php echo $rsedit[pincode]; ?>
<?php echo $rsedit[contact_no]; ?>
<?php echo $rsedit[email_id]; ?>
I am searching and replacing in notepad++. But it takes long time.
I want following output:
<?php echo $rsedit['customer_name']; ?>
<?php echo $rsedit['address']; ?>
<?php echo $rsedit['city']; ?>
<?php echo $rsedit['pincode']; ?>
<?php echo $rsedit['contact_no']; ?>
<?php echo $rsedit['email_id']; ?>
Tell me the best solution for this to replace together.
I would use the following find and replace, in regex mode:
Find: <\?php echo \$(.*?)\[(.*?)\];\s*\?>
Replace: <?php echo \$$1['$2']; ?>
Demo
Provided all those 100 lines always begins with "echo $rsedit"
in regular notepad you could just CTRL+H(windows) or edit > replace
Then add "rsedit[" on find what
Then add "rsedit['" on replace with //notice the single quote after rsedit[
then tap replace all
to add single quote at the end again CTRL+H(windows)
Then add "];" on find what
Then add "'];" on replace with //notice the position of the single quote
then tap replace all
If the variables are different say
<?php echo $rsedit1[customer_name]; ?>
<?php echo $somethingelse[address]; ?>
<?php echo $rsedit3[city]; ?>
<?php echo $rsedi6[pincode]; ?>
<?php echo $ftedit[contact_no]; ?>
<?php echo $rsedit[email_id]; ?>
Just look for something that is common and use it as a replacement point.For the above code you could replace "[" with "['" notice the single quote
then replace ] with ']
IN PHP I noticed that if we have code like below:
<?php if ( function('parameter')):?>
<?php //do something here ?>
<?php endif; ?>
why can't we write this code like:
<?php if ( function('parameter'))
//do something here
endif; ?>
I am new to PHP, Thanks a lot!!
The PHP code has to be inside <?php ?> and the HTML markup needs to be outside. You can also print out the HTML markup with echo.
Here is an example (much cleaner in my opinion, than example 2). The HTML markup is inside a PHP string. The return value of the_field(), a string, is then concated with .:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
echo '<p class="btn">Request a Quote</p>';
}
if(get_field('rfq_pdf_url')) {
echo '<p class="btn">Download PDF</p>';
}
?>
And here is another valid example (2). You can end the PHP part with ?> and output regular HTML markup and then start the PHP part again with <?php:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) { ?>
<p class="btn"><a href="
<?php the_field('quote_url'); ?>
">Request a Quote</a></p>
<?php }
if(get_field('rfq_pdf_url')) { ?>
<p class="btn"><a href="
<?php the_field('rfq_pdf_url');?>
">Download PDF</a></p>
<?php }
?>
It would however be redundant to start with <?php on every line and end it then again with ?>.
Another possibility would be:
<?php
the_post_thumbnail('square');
if(get_field('quote_url')) {
?>
<p class="btn"><a href='<?php echo the_field('quote_url'); ?>'>Request a Quote</a></p>
<?php
}
if(get_field('rfq_pdf_url')) {
?>
<p class="btn">Download PDF</p>
<?php
}
?>
I'm trying to call an HTML/PHP content that it's inside my database using:
<?php echo $row_content['conteudo']; ?>
When the row is called the HTML appears correctly but the PHP doesn't.
I belieave it's cause of the echo inside the main echo.
<?php echo "
<h3>Hello</h3>
<?php do { ?>
<div class=\"indios\">
<a href=\"indio.php?id=<?php echo $row_indiosct['id']; ?>\">
<img src=\"galeria/indios/<?php echo $row_indiosct['foto']; ?>\" alt=\"<?php echo $row_indiosct['nome']; ?>\" />
<br /><?php echo $row_indiosct['nome']; ?></a></div>
<?php } while ($row_indiosct = mysql_fetch_assoc($indiosct)); ?> "
?>
The line one of this code is the same echo as the first code field, it's not repeating, it's there just for help and to understand where is the problem.
I already fixed some quotation marks but it gives an error in the line of the 1st echo.
That is some of the ugliest code I have ever seen...
<?php
echo '
<h3>Hello</h3>';
while ($row_indiosct = mysql_fetch_assoc($indiosct))
{
echo '
<div class="indios">
<a href="indio.php?id='.$row_indiosct['id'].'">
<img src="galeria/indios/'. $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
}
?>
You could also use the HEREDOC syntax.
Don't do this. Multi-line echoes, especially when you've got embedded quotes, quickly become a pain. Use a HEREDOC instead.
<?php
echo <<<EOL
<h3>Hello</h3>
...
<div class"indios">
...
EOL;
and yes, the PHP inside your echo will NOT execute. PHP is not a "recursively executable" language. If you're outputting a string, any php code embedded in that string is not executed - it'll be treated as part of the output, e.g.
echo "<?php echo 'foo' ?>"
is NOT going to output just foo. You'll actually get as output
<?php echo 'foo' ?>
You have misunderstood how PHP works. PHP is processed by the server. When it encounters your script, it sees the following:
<?php echo "some long piece of text that you have told PHP not to look at" ?>
What is the reasoning behind trying to nest PHP calls inside strings?
evaluate code php in string using the function eval(): this post Execute PHP code in a string
<?php
$motto = 'Hello';
$str = '<h1>Welcome</h1><?php echo $motto?><br/>';
eval("?> $str <?php ");
http://codepad.org/ao2PPHN7
also if your need the code buffer output in a string also you can using the ob_start() method:
<?php ob_start(); ?>
<h3>Hello</h3>;
<?php
while ($row_indiosct = mysql_fetch_assoc($indiosct)){ ?>
<div class="indios">
<a href="indio.php?id='<?php echo $row_indiosct['id']'">
<img src="galeria/indios/'<?php echo $row_indiosct['foto'].'" alt="'.$row_indiosct['nome'].'" />
<br />'.$row_indiosct['nome'].'</a>
</div>';
<?php } ?>
I have question, how to placing my html content like this:
<?php
$html =
?>
//in this space, i will place my html
<span></span>
<?php
;
?>
// and i print it
<?php echo $html;?>
Why not just do that between the PHP tags?
<?php
$html = '<span></span>';
echo $html;
?>
You need output buffering for this.
<?php
ob_start();
?>
<!-- your html code here -->
<span></span>
<?php
$html = ob_get_clean();
?>
From what I understand you might want to have a look at heredoc syntax. However, your question is not exactly clear.
<?php
$html = <<<EOT
<span></span>
<!-- You can place anything in here "without escaping" -->
EOT;
echo $html;
?>
<?php
if(get_field('member_only_content')){
echo "do something";
}else{
echo "do something else";
}
?>
How do I insert HTML code where it says "do something"? When removing echo "do something"; and pasting the HTML code the Wordpress site crashes.
Close and open the PHP blocks, like this:
if(get_field('member_only_content')){
?>
<html></html>
<?php
} else{
?>
<html></html>
<?php
}
You can also use PHP's alternative syntax for this:
if(get_field('member_only_content')): ?>
<html></html>
<?php else: ?>
<html></html>
<?php endif;
Like this!
<?php
if(get_field('member_only_content')) : ?>
<div>Html stuff</div>
<?php else : ?>
<div>More Html stuff</div>
<?php endif;
Put HTML in place of the string.
<?php
if(get_field('member_only_content')){
echo "<your>HTML here</your>";
}else{
echo "<other>HTML</other>";
}
?>
You can also break out of the PHP tags
<?php
if(get_field('member_only_content')){
?>
<your>HTML here</your>
<?
}else{
?>
<other>HTML</other>
<?
}
?>
That would be because you placed the HTML code within php tags (<?php ?>). Text inside this tag is interpreted as a PHP instruction, thus it won't render HTML. There are 2 general ways render the HTML in a PHP file:
Echo the HTML
<?php
if (get_field ('member_only_content')) {
echo "<span>Put HTML here</span>";
}
else {
echo "<span>Put HTML here</span>";
}
?>
Place the HTML Outside PHP Tags
<?php if (get_field ('member_only_content')): ?>
<span>Put HTML here</span>
<?php else: ?>
<span>Put HTML here</span>
<?php endif;?>
Or you can use the <<< statement:
echo <<<END
This uses the "here document" syntax to output
multiple lines with $variable interpolation. Note
that the here document terminator must appear on a
line with just a semicolon. no extra whitespace!
END;
Straight from http://php.net/manual/en/function.echo.php
<?php
if(get_field('member_only_content')){
echo "<div style='background-color: #888888'>This is a simple string between div's, make sure you've to be careful while inserting too many single and double quotes in this string as well as inline styles</div>";
}else{
echo "do something else";
}
?>
Be careful while inserting quotes in your string.
Sample code
echo("<h1>This is a sample</h1>");