Welcome Guest, Not a member yet? Register   Sign In
There's a dollar(sign) in variable name

Is that possible to get the data when their is a dollar(sign) in variable.


my column name is price_in_u$
** this is the column name in my database

then i use model and controller to query and get data ( i have no problem here)

this is my code when i display the data
PHP Code:
echo 'Price in US: ' $comp_prod->prince_in_u

the error i got is:
syntax error, unexpected '$', expecting ',' or ';'

is there a way i can use the dollar(sign) ?

even escaping the string is not working.
PHP Code:
echo 'Price in US: ' $comp_prod->prince_in_u/$ 

You could try:

PHP Code:
$tmpVar 'prince_in_u$';
'Price in US: ' $comp_prod->$tmpVar


PHP Code:
echo 'Price in US: ' $comp_prod->{'prince_in_u$'}; 

Another idea would be to fetch the result as an array instead of an object.

PHP Code:
foreach ($query->result_array() as $row)
  echo 'Price in US: '$row['prince_in_u$'];

Or as a last resort you could change the column name in your database.

PS, in your code you forgot the dot sign to concatenate the string and the object.

PHP Code:
$temp_var 'Price_IN_U$';


this one works! hooray! haha i already come up with this idea but i also include the
comp-prod that makes my code echo it.

thanks for this! new learning.

Something else to consider is that you can alias the column name in your select statement, like this:
PHP Code:
// Although I don't recommend adding '*' in the array, 
// I've done so here because I don't know what else you 
// may be selecting in this query.
$this->db->select(array('price_in_u$ as price_in_us''*'));
// ...
echo "Price in US: {$comp_prod->price_in_us}"

Theme © iAndrew 2016 - Forum software by © MyBB