homepage Welcome to WebmasterWorld Guest from 54.227.89.236
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
return $result- can't print it outside function!
yoo_daa




msg:1300306
 2:31 am on Nov 2, 2004 (gmt 0)

Hi there,

I'm learning PHP and having "HUGE" difficulties printing an array string out from a function. Here is a bit of the script.

--------
function ValidateMail($Email) {
global $HTTP_HOST;
$result = array();
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
$result[0]=false;
$result[1]="$Email is not properly formatted";
return $result;
}else{
$result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of function
---------
echo $result[0];
echo $result[1];

I tried to print it inside and it works. But when I'm trying to print(echo) $result[1] outside the function it doesnt work. Nothing shows up. I tried with "global" command but that didnt work.

Please any good suggestions, been trying to find an answer to this problem for 2 days now and I'm frustrated!

Thanks

 

bobnew32




msg:1300307
 3:01 am on Nov 2, 2004 (gmt 0)

print ValidateMail($newstring);

Ever tried that, actually using the function? ^^

mincklerstraat




msg:1300308
 9:05 am on Nov 2, 2004 (gmt 0)

If you put:
global $result;
inside your function, this should work.

This is a problem of variable scope. Variables inside functions have nothing to do with variables of the same name outside of the functions - unless you make them global inside the functions.

Also, return $varname; doesn't do anything in particular at all except for jumping out of the function back to the line of code that called the function, unless you do something like bobnew32 suggests -
$f = ValidateMail($email);
will set $f to this value that is 'returned' (in this case an array, so you'd want to
echo $f[0].', '.$f[1];) - or else like what bobnew32's doing, which will use the value returned from the function as the argument of another function and do whatever that function's supposed to do based on this return value. In that case, though, you'd be better off doing
print_r ValidateMail($newstring); since this returns an array, not a regular variable.

yoo_daa




msg:1300309
 11:09 am on Nov 2, 2004 (gmt 0)

Thank you for your answers.

I tried ,global $result, but nothing happens. I'm aware of the difference of a string, inside a function and outside of it, and in this case its an array. $result(0,1)and so on.

Im sorry to say that I dont understand your answers and how this new function could solve my problem.

Please can u give me an example on how this should look like. Thanks!

mincklerstraat




msg:1300310
 12:20 pm on Nov 2, 2004 (gmt 0)

$check = ValidateMail('me@example.com');
function ValidateMail($Email) {
global $HTTP_HOST, $result; /* $result globalized inside function */
$result = array();
if (!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $Email)) {
$result[0]=false;
$result[1]="$Email is not properly formatted";
return $result;
}else{
$result[0]=true;
$result[1]="$Email appears to be valid.";
return $result;
} // end of else
} // end of function
echo $result[0];
echo $result[1];

works;
$result is globalized inside the function. Will also work if you just print_r($check), even if you don't globalize $result (since $result is returned, and the return value of the function is assigned to $check. Hope this helps!

If it doesn't work in your script, it's quite possible you're having other problems with scope. Try it in a separate file and call that file. Try globalizing $result in other places where it may be relevant.

yoo_daa




msg:1300311
 2:30 pm on Nov 2, 2004 (gmt 0)

mincklerstraat,

Thank you very very very much!

I tried it out this way earlier and it didnt work, must have forgot something.

Anyway, it works now so thank you again, you made may day +++++much better.....:))

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved