Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
variable within a variable
Old 07-08-2006, 04:31 AM variable within a variable
Junior Talker

Posts: 1
Name: Alan Bluwol
Trades: 0
Hey guys,

I'm completely new to php and attempting to program a simple text management system.

I have a database set up, and have stored paragraphs with variable names corresponding to the values I wish to be displayed.

For example:

$name = "Alan";
$p1 = $result['paragraph'];

assume that the variable $p1 has a sentence that i get from the database.
The sentence as it is written in the database is "My name is $name".

When i echo "$p1", I would like the the variable $name to appear in the sentence. i.e. My name is Alan

How would I go about this?
Thanks guys
alanbluwol is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 07-08-2006, 10:35 AM Re: variable within a variable
mgraphic's Avatar
Truth Seeker

Latest Blog Post:
JAMISONTUNES
Posts: 2,918
Name: Keith Marshall
Location: Connecticut
Trades: 0
Using string formats will do exactly what you want it to do:

PHP Code:
<?php
  
  
// The string
  
$name 'Alan';
  
  
// The text string with format recall
  
$p1 'My name is %s';
  
  
// Echo the string
  
printf($p1$name);
  
  
// Outputs: My name is Alan
  
?>
__________________

<mgraphic /> - I don't have a solution but I admire the problem.
mgraphic is offline
Reply With Quote
View Public Profile
 
Old 07-10-2006, 03:31 AM Re: variable within a variable
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Try
PHP Code:
$p1 = eval($result['paragraph']); 
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Old 07-10-2006, 10:03 AM Re: variable within a variable
Novice Talker

Posts: 10
Name: Cody Singsaas
Trades: 0
You could also do....

PHP Code:
 
$name 
"Alan";
 
$sentance "My name is ";
 
$combined $name.$sentance;
 
echo 
$combined
__________________
IE Internet Solutions / Cody Singsaas (President)

Please login or register to view this content. Registration is FREE
from a trusted provider

URL
Please login or register to view this content. Registration is FREE
/ Email
Please login or register to view this content. Registration is FREE
csingsaas is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to variable within a variable
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.83407 seconds with 12 queries