Page 1 of 1

[Strings]

Posted: Sun Mar 12, 2006 4:00 pm
by Fernando Santos
Hi people! the strings will make me crazy!

Code: Select all

<?
$name = 'Fernando';

$res1 = mysql_query( "SELECT t_id, t_text FROM texts HAVING t_id = MIN(t_id)" );
$line1 = mysql_fetch_row($res1);
$phrase = $line1[1];
// variable $phrase come from a record in database
// value of $phrase: 'My name is $name'

echo $phrase
//display 'My name is $name' :cry: 
?>
:?: How to display 'My name is Fernando' ? :?:
Thx!

Posted: Sun Mar 12, 2006 4:03 pm
by Burrito
if you want to include the variable in your quoted string, you need to use double quotes. the other alternative is to concat your variable to your string:

ex:

Code: Select all

$name = "burrito";
echo "my name is $name";

//or use the concat operator (.)

echo 'my name is '.$name;

Posted: Sun Mar 12, 2006 4:12 pm
by Fernando Santos
thx for burrito!

but dont works! assumes that its a table of errors. each phrase is one record and each record with some variables like $user - logged user.

Posted: Sun Mar 12, 2006 6:11 pm
by josh
try using sprintf

if this is the string in the DB:

"my name is %s"


use it like this

Code: Select all

$name = 'jshpro2';
$string = 'My name is %s';
$result = sprintf($string,$name);
echo $result;

Posted: Sun Mar 12, 2006 6:56 pm
by Fernando Santos
Thx I also obtained:

Code: Select all

<? 
$name = 'Fernando'; 

$phrase = 'My name is $name' //simple string because DB

$phrase = "echo \"$phrase\"; ";
eval($phrase);
?>
:twisted:

Posted: Mon Mar 13, 2006 12:44 am
by John Cartwright
That is a serious misuse of the eval() function..

eval() is generally avoided at all costs, unless absolutely required.