PHP echo inside echo - 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 } ?>

Related

How to add single quote at beginning and end of array variable in php using notepad++

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 ']

Dynamic link in IF/ELSE statement

I apologize for any misuse of terminology...I'm a noob...
I have a dynamically created page that contains a dynamic link. I added an IF/ELSE statement to display a different word based on the number of items in the variable $rowsphoto.
The different words display correctly, but the URL that is generated contains all of the PHP instead of generating the correct URL.
This is the original code, which works fine:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">Additional works in Portfolio:</div>
This is the code I have after adding the IF/ELSE statement:
<?php if($portfolioid != 0) { ?>
<div class="extrafield">
<?php
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid;?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid;?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
?>
</div>
I ran the code through a couple syntax checks and they all came back with no errors. What am I doing wrong? Is this even possible?
You'd be better off using it correctly like this:
<?php if($portfolioid != 0): ?>
<div class="extrafield">
<?php if($rowsphoto <= 4): ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Edition:</a>
<?php else: ?>
Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=<?php echo $artistid; ?>&pid=<?php echo $portfolioid; ?>&album=<?php echo $albumid; ?>&id=<?php echo $photoidd; ?>&Itemid=105' class='portfoliocol'>Portfolio:</a>
<?php endif; ?>
</div>
<?php endif; ?>
You're mixing html and php very badly. You should be separating them to keep your code clean and concise.
Your issue with it displaying the php instead of the correct variables is because (as #scrowler said):
You can't use PHP tags inside PHP, you just need to escape the string
bounds and use the . concatenation operator instead of trying to open
new PHP tags, e.g. echo "String here" . $varname; as opposed to echo
"String here"
While Darren's answer is correct, alternatively you can just stay inside of php
<?php
if($portfolioid != 0) {
echo '<div class="extrafield">';
if ($rowsphoto <= 4){
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Edition:</a>";
} else {
echo "Additional works in <a href='index.php?option=com_jartists&view=portfolio&aid=$artistid&pid=$portfolioid&album=$albumid&id=$photoidd&Itemid=105' class='portfoliocol'>Portfolio:</a>";
}
echo '</div>';
}
?>

How to echo a string which contains <?php ?>

I want to generate a php page which contains HTML and some php commands. Problem is that when I submit the code below, I get this output
<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>
rather than php commands. How do I fix this??
Thanks!!
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='0' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo '\$stfromyearErr ';?&gt&lt?php echo '\$stfrommonthErr
';?&gt&lt?php echo '\$stfromdayErr ';?&gt &lt?php echo '\$sttoyearErr ';?
&gt&lt?php echo '\$sttomonthErr ';?&gt&lt?php echo '\$sttodayErr ';?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
You're echoing out your PHP tags as <?php, and then presumably displaying that in a browser. It will LOOK like php code in a browser, because the browsers will render < as < but it's NOT PHP code. it's just some text.
PHP is not recursively executable, e.g.
<?php
echo "<?php echo 'foo '; ?>";
?>
would echo out <, ?, p, etc..., not just foo.
You can do stuff like
<?php
$foo = "<?php echo 'hello world!'; ?>";
file_put_contents('hello.php', $foo);
?>
without any issues. As long as the file you're producing actually gets executed by PHP (e.g. don't name it "hello.html"), then PHP will not know (or even care) that the script was produced by some OTHER php code.
You are encapsulating your variables in single-quotes, therefore they get output as plain text, simply remove the commas on any of your echo statements to fix this.
<?php echo $stfromyearErr; ?>
When you want to output PHP code including tag and even syntax highlighting, you can use the function highlight_string(). Documentation here
<?php
highlight_string("<?php echo '$stfromyearErr '?><?php echo '$stfrommonthErr '?><?php echo '$stfromdayErr '?> <?php echo '$sttoyearErr '?><?php echo '$sttomonthErr '?><?php echo '$sttodayErr '?>");
?>
There are two things, first remove the quotes:
<?php
$page = "<html lang='en'> <head> <meta charset='utf-8' /> </head>
<body><table width='990' border='1' align='center'><tr><td width='54%' colspan='2'>
<span class='error'>&lt?php echo $stfromyearErr ?&gt&lt?php echo $stfrommonthErr
?&gt&lt?php echo $stfromdayErr ?&gt &lt?php echo $sttoyearErr ?
&gt&lt?php echo $sttomonthErr ?&gt&lt?php echo $sttodayErr ?&gt</span></td>
</tr></table> </body></html>";
echo $page;
?>
However, this will give you:
<?php echo your_var_1 ?><?php echo your_var_2 ?><?php echo your_var_3 ?> <?php echo your_var_4 ? ><?php echo your_var_5 ?><?php echo your_var_6 ?>
This is because the php tags are redundant: you have already started php and are echoing these out within php.
If this is unavoidable, try replacing them like this:
echo str_replace(" ?&gt", "", str_replace("&lt?php echo", "", $page));

put string in variable using <? ?>

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.

php using special symbols

<?php do { ?>
<?php echo ""; ?><?php echo $row_pageDetails['name']; ?>(<?php echo $row_pageDetails['profile']; ?>) </br>
<?php } while ($row_pageDetails = mysql_fetch_assoc($rspageDetails)); ?>
This gives a clickable link name(profile) but if the profile is empty It shows () how can I improve it so that when the profile record is empty it shows nothing.
You have many unnecessary opening and closing php tags. You should only use one for this whole thing given your code.
And you have a mis-closed </br> tag, should be <br/> and it would be better if you put it after the closing anchor tag.
You can not show the link at all by putting the whole thing in an if statement
<?php
do {
if(!empty($row_pageDetails['profile'])){
echo "<a href=\"$row_pageDetails[website]\">";
echo $row_pageDetails['name'] . "($row_pageDetails[profile])</a><br/>";
}
} while ($row_pageDetails = mysql_fetch_assoc($rspageDetails));
?>
instead of
(<?php echo $row_pageDetails['profile']; ?>)
use ternary operator
<?php echo ($row_pageDetails['profile']) ? '('.$row_pageDetails['profile'].')' : ''; ?>
Your code must be something like this
<?php do {
echo (isset($row_pageDetails['profile']) && !empty($row_pageDetails['profile']))?
''.$row_pageDetails['name'].'('.$row_pageDetails['profile'].')':''; } while ($row_pageDetails = mysql_fetch_assoc($rspageDetails)); ?>

Categories