Defining a variable.. Within a variable.

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
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Defining a variable.. Within a variable.

Post by Jim »

Code: Select all

$text = "
		'$type' = "<a href='/netnews/news.php?action=add&site=$site'>$site</a>";
		
		if($site != 0) &#123;
		echo '<a href='/netnews/news.php?action=add&site=$site'>$site</a>'
		&#125;";
That code is written a specific file each time a script is run. However, the variable "$type" is never defined within the file to which I've written. I always get blank spots :(

Any ideas?
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

Code: Select all

$text = "
      '$type' = "<a href='/netnews/news.php?action=add&site=$site'>$site</a>";
Esacpe the second quotation-mark like this \"

So your code would read:

Code: Select all

$text = "
      '$type' = "<a href='/netnews/news.php?action=add&site=$site'>$site</a>"";
On the other hand: PHP only compiles once. Meaning: if you write these file-contents to a file stored on your server which then is requested by a browser, it'll work. However, if you create this file on the fly - it won't. Reason: PHP only compiles once.
evilcoder
Forum Contributor
Posts: 345
Joined: Tue Dec 17, 2002 5:37 am
Location: Sydney, Australia

Post by evilcoder »

where are you outputting the data you have stored in the type variable?
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

All of this is going in to a file named "commands.txt". Here's the entire snippet:

Code: Select all

$fp = fopen("/home/empirega/public_html/netnews/commands.txt","w");
$text = "
		'$type' = '<a href='/netnews/news.php?action=add&site=$site'>$site</a>';
		
		if($site != 0) {
		echo '<a href='/netnews/news.php?action=add&site=$site'>$site</a>'
		}";
		
fputs($fp, $text, strlen($text));
fflush($fp);
fclose($fp);
Any further thoughts?
Jim
Forum Contributor
Posts: 238
Joined: Fri Apr 19, 2002 5:26 am
Location: Near Austin, Texas

Post by Jim »

Never mind. I fixed it. I defined the variable OUTSIDE that writing script and used it within the code. Worked :D
Post Reply