CodeIgniter Forums
There's a dollar(sign) in variable name - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forum-5.html)
+--- Forum: General Help (https://forum.codeigniter.com/forum-24.html)
+--- Thread: There's a dollar(sign) in variable name (/thread-62941.html)



There's a dollar(sign) in variable name - rhyszz - 09-09-2015

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

Example:

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/$ 



RE: There's a dollar(sign) in variable name - Diederik - 09-10-2015

You could try:

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

Or

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.


RE: There's a dollar(sign) in variable name - rhyszz - 09-10-2015

PHP Code:
$temp_var 'Price_IN_U$';

$comp-prod->$temp_var

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.


RE: There's a dollar(sign) in variable name - mwhitney - 09-10-2015

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