Retain original value if function fails to match?

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
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Retain original value if function fails to match?

Post by JAB Creations »

This is continuation from the previous thread 'Create a variable with the name of a function's parameter?' however I think the goal for that thread was answered and this would be acceptable for a new thread (as it's a new goal).

So below I have an $audio variable with a default value of '0'. The issue is trying to retain the value of $audio as '0' if the function does not match (decide that the default should be overridden).

I'm thinking either it will require subjectively calling the function or manipulating the function itself somehow. Essentially the valuable thing I've learned of late to avoid using globals (if you've followed my other threads of late) is that a function is like an advanced variable in PHP, you can echo both a variable and a function in the same way. So with in my mind I'm wondering if I can list the defaults in a final else argument (just else {}) and then return the function. That is really all I can think of at this moment in time though I'm open to any constructive ideas. :mrgreen:

Code: Select all

<?php
$audio = '0';
 
function example($property)
{
 if ($_SERVER['REQUEST_METHOD'] == 'GET')
  {
   if (isset($_GET[$property])) {$property = $_GET[$property]; return $property;}
  }
}
 
 
$cookie_old = array(
'audio',
'connection',
'theme'
);
 
foreach ($cookie_old as $key => $value) {
 example($value);
 // Transition Preview IV cookies to Preview V
 if (isset($_COOKIE[$value])) {setcookie($value, '', time()-60); $found = 1;}
 
 // Create $variable
 $$value = example($value);
}
?>
<html>
<head>
</head>
 
<body>
 
<div>
$audio = <?php echo $audio;?>
</div>
 
<div>
<a href="cookie6.php">No Request</a>
<br />
<a href="cookie6.php?audio=0">audio = 0</a>
<br />
<a href="cookie6.php?audio=1">audio = 1</a>
<br />
<a href="cookie6.php?audio=2">audio = 2</a>
</div>
 
</body>
</html>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Retain original value if function fails to match?

Post by JAB Creations »

Input on the following solution keeping in mind that my site/work unconditionally uses classes and not variables in regards to preferences...

Code: Select all

<?php
$audio = '0';
 
function example($property)
{
 if (isset($_GET[$property]))
  {
   $property = $_GET[$property]; return $property;
  }
  else
  {
   global $$property;
   return $$property;
 }
}
 
$cookie_old = array(
'audio',
'connection',
'theme'
);
 
foreach ($cookie_old as $key => $value) {
 example($value);
 // Transition Preview IV cookies to Preview V
 if (isset($_COOKIE[$value])) {setcookie($value, '', time()-60); $found = 1;}
 
 // Create $variable
 $$value = example($value);
}
?>
<html>
<head>
</head>
 
<body>
 
<div>
$audio = <?php echo $audio;?>
</div>
 
<div>
<a href="cookie6.php">No Request</a>
<br />
<a href="cookie6.php?audio=0">audio = 0</a>
<br />
<a href="cookie6.php?audio=1">audio = 1</a>
<br />
<a href="cookie6.php?audio=2">audio = 2</a>
</div>
 
</body>
</html>
Post Reply