PHP Variable Inside Echo 'HTML Code'

How do I echo a php variable inside html tags inside an if statement?

If you want to echo PHP variable inside echo function, you must as first break echo with ", then add a "merge char" ., then your variable and then again "merge char" and ".

Also don't forget to add semicolon to end of your line which contains definition of $_SESSION['age'].

Edit your code to:

 <?php

$_SESSION['age'] = 11;

if($_SESSION['age'] < 14) {
echo "<h2>Your only ".$_SESSION['age']."? Thats Young!</h2>";
}
?>

Using PHP variables inside HTML tags?

Well, for starters, you might not wanna overuse echo, because (as is the problem in your case) you can very easily make mistakes on quotation marks.

This would fix your problem:

echo "<a href=\"http://www.whatever.com/$param\">Click Here</a>";

but you should really do this

<?php
$param = "test";
?>
<a href="http://www.whatever.com/<?php echo $param; ?>">Click Here</a>

echo HTML with php variable

Try this.. run in your php enviroment

<html>
<head>
<style type="text/css">
p
{
text-align: center;
color: red;
}
</style>
</head>
<body>
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
</body>

</html>

php variable inside echo 'html code'

You concatenate the string by ending it and starting it again:

echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';

Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:

?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php

How do i echo variable inside html

Don't write PHP inside PHP. change your code to:

<?php echo'<TABLE BORDER="0" cellpadding="0" CELLSPACING="0"> <TR> <TD WIDTH="60"
HEIGHT="70" BACKGROUND="inventory_images/3.jpg" VALIGN="bottom"> <FONT SIZE="+5"
COLOR="red">'.$cartTotal.'</FONT></TD> </TR> </TABLE>'; ?>

PHP echo variable and html together

<?php
$variable = 'testing';
echo <span class="label-[variable]">[variable]</span>
?>

Should be

<?php
$variable = 'testing';
echo '<span class="label-' . $variable .'">' . $variable . '</span>';
?>

How do I echo html tags in PHP Variable?

HTML tags will automatically parsed by the browser. To see the pure HTML source anyways, the tags needs to be encoded, especially the angle brackets.

There is a dedicated PHP function available for that called htmlentities().

$var = "Hello<br>How are <strong>you</strong>";
echo htmlentitites($var);

Will encode it in the browser so you can see it displayed as plain text like in $var. The browser will see this:

Hello<br>How are <strong>you</strong>

PHP variables inside HTML Code

as you're in PHP, so you don't need to open and close PHP tag.

The reason you're getting Syntax error is just because you're not manipulating string properly.

error is with this line

echo '<input type="radio" name="department" value=<?php $row['DEPT_ID'] ?>>';              
^ here ^ here

So you need to remove the PHP tags and need to concatenate string properly like:

echo '<input type="radio" name="department" value="'.$row['DEPT_ID']. '">';

and with this one

echo </form>;

you're missing quotes around form tag. So it should be,

echo '</form>';

There are some other typos are as well, so your final code will be look like this.

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
//echo "<br />"; add this <br /> tag to next echo
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
//or you can do this way
//echo "<input type='radio' name='department' value='$row[DEPT_ID]'><br />";
//echo "<br />"; appended in upper statement.
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
//echo </form>; closed already(above statement).

}

and without comments, more cleaner :)

$ShowPossibleDep = mysql_query("SELECT NAME,DEPT_ID FROM DEPARTMENT");
if(mysql_num_rows($ShowPossibleDep) > 0){
echo "<br />Available departments: ".mysql_num_rows($ShowPossibleDep);
echo '<br /><form id = "dept" action = "Courses.php" method = "post">';
while($row = mysql_fetch_array($ShowPossibleDep))
{
echo $row['NAME'];
echo '<input type="radio" name="department" value=" '.$row['DEPT_ID'].'"><br />';
}
echo '<input type = "submit" value = "Submit" id = "submitDepartment"></form>';
}

PHP: How can I put HTML Code with PHP Code within into a PHP Variable?

<?php ... <?php

Since your string contains PHP tags, I suppose you expect them to be evaluated. The opening PHP tag within another PHP tag is interpreted as a part of the PHP code. For example, the following outputs <?php echo time();:

<?php echo "<?php echo time();";

There are several ways to build a PHP string from PHP expressions.

Concatenation

You can create functions returning strings and concatenate the calls to them with other strings:

function some_function() {
return time();
}

$html = "<li " . some_function() . ">";

or use sprintf:

$html = sprintf('<li %s>', some_function());

eval

Another way is to use eval, but I wouldn't recommend it as it allows execution of arbitrary PHP code and may cause unexpected behavior.

Output Buffering

If you are running PHP as a template engine, you can use the output control functions, e.g.:

<?php ob_start(); ?>

<li data-time="<?= time() ?>"> ...</li>

<?php
$html = ob_get_contents();
ob_end_clean();
echo $html;

Output

<li data-time="1483433793"> ...</li>

Here Document Syntax

If, however, the string is supposed to be assigned as is, use the here document syntax:

$html = <<<'HTML'
<li data-time="{$variable_will_NOT_be_parsed}">...</li>
HTML;

or

$html = <<<HTML
<li data-time="{$variable_WILL_be_parsed}">...</li>
HTML;


Related Topics



Leave a reply



Submit