Jump to content

[SOLVED] quite tricky, .. can you please help me ..


Recommended Posts

 

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

 

 

 

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"
?>

<?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.

 

 

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.