PHP beginner help - avoiding spaghetti code

Associate
Joined
24 Jul 2003
Posts
1,420
Location
West Mids
Ok, I'm completely new to PHP (usually use .NET) and I decided to do a small project to familiarise myself with it :)

I want to use function variables in the rest of my web page, but I'm not sure how to do it without defining a global variable.

Code:
<?php

function myFunction()
{
   $myVar = "Hello World"; 
}
?>

<div>
   <?php echo $myVar; ?>
</div>

Basically, is it possible to get at the variable value from the function without setting a global variable?
 
only by reference or global, or returning the value.
PHP:
<?php
function myFunction (&$myVar) {
    $myVar = 'HiHi!';
}
?>
<div>
  <?php $aVar = ''; myFunction($aVar); echo $aVar; ?>
</div>
 
Back
Top Bottom