jaikar Posted March 19, 2007 Share Posted March 19, 2007 Hi .. i want a code to do something... but not sure if its possible .. can you please help ... <?php $abc = '$test'; $test = "hello"; $xyz = $abc; echo $xyz; ?> from the above code .. the result should print " hello " ... is this possible ????? my actual need is .. i need to store a query in the database where some conditions are dynamic. so it will like " select * from table where id=$id " now the whole thing should be inside the database so when i fetch the $id shoulld act as a variable .... PLease adive.. Thanks Jaikar Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/ Share on other sites More sharing options...
wildteen88 Posted March 19, 2007 Share Posted March 19, 2007 You can only call variables from within double quotes and not single quotes. If you are storing PHP code in a database then you will need to use eval() in order to process the PHP code in the database. Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210709 Share on other sites More sharing options...
flappy_warbucks Posted March 19, 2007 Share Posted March 19, 2007 Well... lets look at the code shall we: <?php $abc = '$test'; // litral string meaning it prints whats in the quotes and not whats in the varable, this is where your going wrong. $test = "hello"; // this is not a litral sting as it has the " and not the ' so that would print out what in the varible as well as the litral text. $xyz = $abc; // here you are assigning $xyz with the contents of $abc echo $xyz; // and here you are printing out the litral string: "$test" ?> Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210711 Share on other sites More sharing options...
per1os Posted March 19, 2007 Share Posted March 19, 2007 <?php $test = "hello"; // Variable positions is key. $abc = $test; // do not know why you are quoting it $xyz = $abc; // should be what it is. echo $xyz; // Could be done like this also: $test = "hello"; // Variable positions is key. $abc = "$test"; // shown as quoted, must be double to do the $ $xyz = $abc; // should be what it is. echo $xyz; ?> If you want it to be like how you have it you may need to look into reference/pointers. I never got the hang of pointers, but I am sure that is what you want if you want the variable $abc to change after declaration. Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210714 Share on other sites More sharing options...
jaikar Posted March 19, 2007 Author Share Posted March 19, 2007 Thanks a lot Friends..... flappy_warbucks, ... your explanation is absolutly correct and that is what was in my mind .. what i need is .. when i print the literal "$test" i wanted it to act as a variable and should print the value inside the $test.. i think wildteen88 gave a good idea and now i will try it .. frost110 , ..... i did not used the single quote because, if i store the $test in database ??... it will act as literal correct ?.. Thanks again !!.... Jaikar Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210722 Share on other sites More sharing options...
per1os Posted March 19, 2007 Share Posted March 19, 2007 If you want to store it in the DB as $test you want to use single quotes. If you want it to display the value you use double quotes. $test = "Fun!"; $tst = "$test"; print $tst; // should print Fun! $tst2 = '$test'; print $tst2; // should print $test Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210756 Share on other sites More sharing options...
jaikar Posted March 20, 2007 Author Share Posted March 20, 2007 thanks froast, the concern is when retrieving the variable from database.... storing is not a problem... anyhow, the answer is using the eval()... jaikar Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-210998 Share on other sites More sharing options...
wildteen88 Posted March 20, 2007 Share Posted March 20, 2007 That is correct. PHP will treat any PHP code coming out of a database literally. As when you fetch something from the database PHP will treat it as a string, depending on the data type you use for your columns. Quote Link to comment https://forums.phpfreaks.com/topic/43388-solved-quite-tricky-can-you-please-help-me/#findComment-211409 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.