setting vars in function

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

setting vars in function

Post by John Cartwright »

functions.php

Code: Select all

<?php
		function svar($var,$condition)
		{
			$vars["{$var}"] = $condition;
			return $vars[$var];
		}
?>
and on another file which inludes functions.php

Code: Select all

<?php
		$varcontrol->svar("article_header",$row["title"]);
?>
but I'm having sort of a brain bubble.
I know the function will delete all the vars it creates at the end of a function but how do I get this var to remain set, so it acts like it was set by $vars["varname"]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Can't you just do it like this?

Code: Select all

<?php
      function svar($var,$condition) 
      { 
         $vars["$var"] = $condition; 
         return $vars[$var]; 
      } 
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

oops I did have the extra {} but that wouldn't solve anything.

$var[$varname] will never be set outside the function
Post Reply