[Strings]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Fernando Santos
Forum Newbie
Posts: 3
Joined: Sun Mar 12, 2006 3:34 pm

[Strings]

Post 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!
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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;
Fernando Santos
Forum Newbie
Posts: 3
Joined: Sun Mar 12, 2006 3:34 pm

Post 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.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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;
Fernando Santos
Forum Newbie
Posts: 3
Joined: Sun Mar 12, 2006 3:34 pm

Post 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:
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

That is a serious misuse of the eval() function..

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