Passing php data to HTML

0 iftikhar ali · May 6, 2015
Hi everyone,
Im trying working on a small project in which I'm trying to use data in PHP which is stored in a variable and want to use it in html code. Both the PHP and HTML codes are saved in separate files and I used "include()" function to use the PHP file in HTML. I have tried so many things shown in turorials but none of them work.Can anyone help???

 my code is as follows:

PHP code:

$var = "any string";

function getVariable(){
echo "<h4>$var</h4>";

HTML code:


<?php getVariable(); ?>


Post a Reply


Oldest  Newest  Rating
0 iftikhar ali · May 8, 2015
Thanks Mirohamadi!
+1 Mohamad Mirohamadi · May 6, 2015
 :angry::angry: God damn it !!!

< ? p h p     e c h o      "<h4>$var</h4>" ;    ? >
0 Mohamad Mirohamadi · May 6, 2015
Why I can't edit my prev post :(
//In html code
0 Mohamad Mirohamadi · May 6, 2015
The first code is:
//In html code
+1 Mohamad Mirohamadi · May 6, 2015
The variable $var  isn't in the scope of getVariable() function.
so when you call the function there will be a notice that $var is not defined.
you can simply use:
//In html code

or  you can pass $var to the function

//modify the function like below:
function getVariable($str){
echo "$str";
//In html code use this:

or you can use Globals variable like following:

$GLOBALS['var'] = "my string...";
function getVariable()
  echo "".$GLOBALS['var']."";

or you can define Constance which doesn't mean here.
  • 1



Server-side, HTML embedded scripting language used to create dynamic Web pages.