How to echo a string which contains <?php ?> - 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));

Related

How to get image URL to display in PHP

Can't get image URL to display in my code; this is my URL: https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
<!DOCTYPE html>
<html>
<head>
<title>PHP Exercise 1: Links and Variables</title>
</head>
<body>
<h1>PHP Exercise 1: Links and Variables</h1>
<p>Use PHP echo and variables to output the
following link information:</p>
<hr>
<?php
$linkName ='<h1> Codecademy <h1>';
$linkURL = 'codecademy';
$linkImage =
'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
$linkDescription = 'Learn to code interactively, for free.';
$my_name = "peter";
echo "<img>" . $linkImage . "</img>";
echo $linkName;
echo "<br>";
echo $linkURL;
echo "<br>";
echo $linkImage;
echo "<br>";
echo $linkDescription;
$linkImage = 'https://production.cdmycdn.com/webpack/renderer/d7285ffbbd0ca6d1d2179f7d22ea1f67.svg';
echo '<img src="data:image/jpeg;base64,">';
?>
</body>
</html>
A link to an image should always be put in the src attribute when you're using the img tag in html.
That means you should be looking for something like "<img src=".$linkImage."></img>" instead of "<img>".$linkImage."</img>"
Of course, that only applies if the problem you're referring to is the fact that there should be an image after the sentence "Learn to code interactively, for free."
Works just fine for me. Try checking php permissions.

Retrieve blob image into html php

Display image from mysql.
<?php
if(isset($id1)){
$query="SELECT * FROM `newss` WHERE `id`=".$id1;
$result=mysql_query($query);
if(!$result){
echo "<h3>News Not Availabel</h3>";
}else{
while($row=mysql_fetch_array($result)){
echo "
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
<figure class=date>
<h2>".$row['description']."</h2>
<div class=blog-detail-meta>
<span class=date><span class=fa fa-file-o></span>".$row['dates']."</span>
</div>
</header>
<hr>
<p>".$row['para1']." </p>
<p>
".$row['para2']."
</p>
<p>
".$row['para3']."
</p>
";
}
}
}else{
header("location:index.php");
}
?>
Image not properly displayed.
I stored raw data blob (png format). I want to display image, but only symbols are displayed.
Try
echo "<img src=\"data:image/png;base64,".$row['imges']."\" />" ;
Instead of
echo "<img src=data:image/png;<?php echo ".$row['imges']."; ?> />" ;
Is the table name really "newss"?
Don't do things like:
echo "
HTML CODE
";
Do this:
?>
HTML CODE <?php echo $php_code;?>
<?php
You currently trying to use PHP opening Tag <?php within PHP self.
<?php
echo "<?php echo 'test';?>";
?>
PHP won't parse this, but will just echo it as pure text.
Change following line:
<img src=data:image/png;<?php echo ".$row['imges']."; ?> />
to:
<img src="data:image/png;".$row['imges'].";"/>
Also use double quotes for HTML-Parameters like src="path/to/file.format".
Not being lazyis very important in coding!
If everything does not work, what is the output of $row['imges'] ?

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>';
}
?>

PHP echo inside echo

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 } ?>

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