Showing posts with label variable. Show all posts
Showing posts with label variable. Show all posts

Thursday, May 2, 2019

Dynamic PHP variable


PHP allows you to use dynamic variable names, called variable variables. You can name a variable with the value stored in another variable. That is, one variable contains the name of another variable.


$Bangladesh = 147570;
$dhaka = 11500;
$cityname = "Bangladesh";
echo "The size of $cityname is ${$cityname}";
$cityname = "dhaka";
echo "The size of $cityname is ${$cityname}";
The output from this code is
The size of Bangladesh is 147570
The size of dhaka is 11500

Ref: https://www.dummies.com/programming/php/how-to-use-php-variable-variables/

Sunday, October 1, 2017

Store php variable in mysql / dynamic email, SMS template

This is a good tutorial if we want to create dynamic Email Template/ Dynamic SMS Template or dynamic content using php mysql. Storing php variable in content and save to mysql database. when fetching data from database replace variable with value dynamically.

 First create a table as your require.  Suppose we want to store this kind of sms template in datasbse

 <p>  
 Hello : [FULL_NAME] <br/>   
 username: [USER_NAME] <br/>   
 email : [USER_EMAIL] <br/>  
 website : [SITE_URL]  
 </p>  

Now we can fetch this data from database and assign in a php variable

 $content = $row['content'];  
 //replace template var with value  
 $token = array(  
   'FULL_NAME' => 'Sharif Ahmed',  
   'USER_NAME' => 'winsharif',  
   'SITE_URL' => 'http://www.esteemcorporation.com',  
   'USER_EMAIL'=> 'winsharif@test.com'  
 );  
 $pattern = '[%s]';  
 foreach($token as $key=>$val){  
   $repVar[sprintf($pattern,$key)] = $val;  
 }  
 $SMSContent = strtr($content,$repVar);  
 echo $SMSContent;  

/////////////////////////////////////Output will be //////////////////////
Hello : Sharif Ahmed 
username: winsharif
email : http://www.esteemcorporation.com 
website :  winsharif@test.com

Total Pageviews