Page 1 of 1

Problem with Variables

Posted: Thu Aug 14, 2008 6:21 pm
by Kedaeus
I have an include full of functions to validate form data.
I pass information to the function - Great..
Returning information (such as an error code) isn't working for me.. Dunno why.

Example: record2.php

Code: Select all

 
include ('includes/inc.funct.valid.php');
foreach (array_keys($_POST) as $key)
    {
        switch ($_POST[$key])
            {
                case "Phone";
                    $$key = $_POST[$key];
                    PHNCHK('primary phone', $_POST[$key], 'text');
                break;
                ...
            }
        $$key = $_POST[$key];
     }
 
Example: inc.funct.valid.php

Code: Select all

 
function PHNCHK ($field, $challenge, $type)
    {
        $position = strpos($challenge, "-");
        if ($position === false)
            {
                $errmsg = 1;
                echo "Your $field is formatted improperly. Format: ###-###-####.";
                return $errmsg;
            }
         if ($pos === 3) //###-...
            {
                ... // left out to shorten script
            }
         else
            {
                return;
            }
    }
 
This is a really dumbed down version of my script.. however the problem lies in the function PHNCHK. When an error is thrown (improper format) my variable "$errmsg" isn't being passed through the functions return statement.. Why?

Re: Problem with Variables

Posted: Thu Aug 14, 2008 9:49 pm
by Zzarkc-20
I'm not sure I'll be of much help, but do you have anything being set to receive the value of phncheck()? I'm talking like when you call it, is it like

$var=phncheck();

That may be your problem. Another option I have used when I get too frustrated is to just make a variable global in the function.

Hope that helps!

Re: Problem with Variables

Posted: Fri Aug 15, 2008 1:55 am
by nowaydown1
As Zzarkc-20 mentioned, in the example you posted you aren't storing the return value of your function call. You need to give it a variable to store your return in. Something like:

Code: Select all

 
$returnValue = PHNCHK('primary phone', $_POST[$key], 'text');
 
Hope that helps.

Re: Problem with Variables

Posted: Fri Aug 15, 2008 9:19 am
by Kedaeus
Wow, that was simple.

Thank you for your help "NoWayDown".. I've been messing around with this database application for a week or so now and thus far it's been the only problem I've come across.. None of the tutorials I've found on variables say anything about setting a variable to the functions.

All the functions up until now had no need to return anything - since I use objects to store information gathered from the database

To the previous poster - I got it working using globals before however I wanted to do it "right" instead of making my debug/errhandling variables global.

Anyhoo, got to get back to work.
Thank you for your assistance,
-Kedaeus