Jump to content

Taking value from PHP into HTML in the same file

I'm not exactly sure how to phrase this, but I need help figuring out how to use the value of one of my variables in PHP code in the HTML code that is running inside of it. For example:

<?php
echo "There is a $serverstatus alert on the $servername server where your $productname product is hosted.";

?>
<html>
  <body>
    Lots of html code using the three variables within the echo above.
  </body>
</html>
<?

echo "Back to PHP to finish up the rest of what I was doing.
?>

How can I get the variables from the echo statement into the HTML code?

Link to comment
Share on other sites

Link to post
Share on other sites

You can echo HTML tags and that will be interpreted correctly, you can also use PHP code inside of HTML tags. Examples

<?php 
$uniqueID = 42;
$product = 'Soy Sauce';

echo '<div id="' . $uniqueID . '">' . $product . '</div>';
?>
<div id="<?php echo '$uniqueID';?><?php echo '$product';?></div>"

 

As I am writing this it occurs to me that you may be asking about concatenation, what I did in the first code snippet with the full stops and all that. There are two ways to insert your variables into strings. There is the way you have done in your example, where the string is in double quotes. Doing it this way means all you have to do with your HTML tags when you echo them, is use them with this syntax <div id='stuff'></div> rather than <div id="stuff"></div>. Then you have the way I have done it, where the echo statement is in single quotes. Both work, I just prefer doing the full stop way because your text editor code highlights variables and I think it looks cleaner. It's also a tad faster if you happen to be printing out 5 million variables.

 

Orrrrrr, your question could have nothing to do with concatenation, in which case, I hope the way I've answered things helps you rephrase your question  

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

move the echo to inside the body tag, remember you have to save the file as .php and have a server running php for it to work.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Hazy125 said:

You can echo HTML tags and that will be interpreted correctly, you can also use PHP code inside of HTML tags. Examples


<?php 
$uniqueID = 42;
$product = 'Soy Sauce';

echo '<div id="' . $uniqueID . '">' . $product . '</div>';
?>

<div id="<?php echo '$uniqueID';?><?php echo '$product';?></div>"

 

As I am writing this it occurs to me that you may be asking about concatenation, what I did in the first code snippet with the full stops and all that. There are two ways to insert your variables into strings. There is the way you have done in your example, where the string is in double quotes. Doing it this way means all you have to do with your HTML tags when you echo them, is use them with this syntax <div id='stuff'></div> rather than <div id="stuff"></div>. Then you have the way I have done it, where the echo statement is in single quotes. Both work, I just prefer doing the full stop way because your text editor code highlights variables and I think it looks cleaner. It's also a tad faster if you happen to be printing out 5 million variables.

 

Orrrrrr, your question could have nothing to do with concatenation, in which case, I hope the way I've answered things helps you rephrase your question  

Maybe I should just say it like this. The html code that I need to put in looks something like the code you can find at http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_alerts. The reason I need the values from the PHP variables is because I need to echo that line inside of one of the alert boxes in the html. Is there a simple way to do that?

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Hazy125 said:

You can echo HTML tags and that will be interpreted correctly, you can also use PHP code inside of HTML tags. Examples


<?php 
$uniqueID = 42;
$product = 'Soy Sauce';

echo '<div id="' . $uniqueID . '">' . $product . '</div>';
?>

<div id="<?php echo '$uniqueID';?><?php echo '$product';?></div>"

 

As I am writing this it occurs to me that you may be asking about concatenation, what I did in the first code snippet with the full stops and all that. There are two ways to insert your variables into strings. There is the way you have done in your example, where the string is in double quotes. Doing it this way means all you have to do with your HTML tags when you echo them, is use them with this syntax <div id='stuff'></div> rather than <div id="stuff"></div>. Then you have the way I have done it, where the echo statement is in single quotes. Both work, I just prefer doing the full stop way because your text editor code highlights variables and I think it looks cleaner. It's also a tad faster if you happen to be printing out 5 million variables.

 

Orrrrrr, your question could have nothing to do with concatenation, in which case, I hope the way I've answered things helps you rephrase your question  

short tags :)

<?=

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, Lunar Evolution said:

Maybe I should just say it like this. The html code that I need to put in looks something like the code you can find at http://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_alerts. The reason I need the values from the PHP variables is because I need to echo that line inside of one of the alert boxes in the html. Is there a simple way to do that?

Unless you're wanting to do it without requiring a page refresh, what I said above should be what you're after

 

<div class="alert info">
  <span class="closebtn">&times;</span>
  <strong>Info!</strong> <?php echo "There is a $serverstatus alert on the $servername server where your $productname product is hosted."; ?>
</div>

 

4 hours ago, prolemur said:

short tags :)


<?=

 

Still not enabled on all servers for some reason, did magic quotes break it or something? No idea, can't remember 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

18 hours ago, Hazy125 said:

Unless you're wanting to do it without requiring a page refresh, what I said above should be what you're after

 


<div class="alert info">
  <span class="closebtn">&times;</span>
  <strong>Info!</strong> <?php echo "There is a $serverstatus alert on the $servername server where your $productname product is hosted."; ?>
</div>

 

Still not enabled on all servers for some reason, did magic quotes break it or something? No idea, can't remember 

No clue, but i think you need at least php 5.4 to use short tags. which is the same version magic quotes got deprecated. coincidence? i think not!

Link to comment
Share on other sites

Link to post
Share on other sites

If you use  " characters, php parses the strings and does replacements inside the text automatically

 

$number = 3;

echo "John has $number eggs.";

 

The output will be "John has 3 eggs."

Note though that not just variables are replaced when you're using " character, for example if you want to echo " on screen you kinda have to write \"  to escape the character and other characters have special meanings so those have to be escaped as well ... so it's always a good idea to make it a habit of NOT using this " character to form strings.

 

If you use the ' character, php won't do any replacements in the strings. ex echo 'John has $number eggs'; will output exactly that.

You can still output stuff by concatenating strings, for example  echo 'John has ' . $number . ' eggs.';

 

You can also use string replacement functions to introduce things into strings in a very fast and simple manner, for example

 

$message =  'A %genre% named %name% walks into a bar and orders a %drink%';

 

Right before outputting this to the user you can say something like this :

 

$message  = str_ireplace(  array( '%genre%' , '%name' , '%drink%'), array ( 'boy' , 'Tom', 'beer'), $message);

echo $message;

 

which say "search in the variable called message any of the keywords in the first array ignoring the letter case (the i in ireplace means insensitive ), if a keyword is found replace with the matching word from the second array , put the results back in the variable $message.

 

ps. you should make it a habit of always using <?php  and ?>  tags, newer php versions by default only allow this long notation, so if you want to make some code as usable as possible on other peoples' computers it's a good idea to write your code as compatible and safe as possible.

 

 

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×