Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 07-06-2006, 02:50 AM
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Posts: 1,324
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default variable not returning in function

I'm sort of copying vBulletin's code that cleans data. If I echo the variable in the function it works but if I return the variable and echo it outside the function it doesn't.

Here is the code.
PHP Code:
<?php


function clean($mthd$arr)
{
    
    if(
$mthd == "p")
    {
        
        foreach (
array_keys($arr) as $val
        {
               
go_clean($val'p');
        }
    }
}

function 
go_clean(&$val$mthd)
{
    
    
$efinfo = array();
    
    if(
$mthd == "p")
    {
      
$efinfo[$val] = $_POST[$val];
    }
      
     return 
$efinfo
     
     
//echo "$val: ". $_POST[$val] ."<br /> ";
    
}

$_POST['username'] = "harmor";
$_POST['password'] = "andrew123";

$data = array('username' => 'STR''password' => 'STR''age' => 'INT''email' => 'STR');
clean('p'$data);

echo 
$efinfo['username'];


?>
What does the "and" symbol in this function do? go_clean(&$val, $mthd)
I just added it to see if it changed anything but it didn't.
Reply With Quote
  #2  
Old 07-14-2006, 07:10 PM
Chris Graham Chris Graham is offline
 
Join Date: Aug 2004
Posts: 2
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I take it you're learning to code as this 'cleaning code' does absolutely nothing . Here's something that will 'clean' PHP's magic quotes...

Code:
<?php


function clean($mthd, $arr)
{
    
    if($mthd == "p")
    {
        
        $efinfo = array();
        foreach (array_keys($arr) as $val) 
        {
               $efinfo = array_merge($efinfo, go_clean($val, 'p'));
        }
        return $efinfo;
    }
}

function go_clean(&$val, $mthd)
{
    
    $efinfo = array();
    
    if($mthd == "p")
    {
      $efinfo[$val] = $_POST[$val];
      if (get_magic_quotes_gpc()) $efinfo[$val]=stripslashes($efinfo[$val]);
    }
      
     return $efinfo; 
     
     //echo "$val: ". $_POST[$val] ."<br /> ";
    
}

$_POST['username'] = "harmor";
$_POST['password'] = "andrew123";

$data = array('username' => 'STR', 'password' => 'STR', 'age' => 'INT', 'email' => 'STR');
$efinfo = clean('p', $data);

echo $efinfo['username'];


?>
Note the key differences:
- we're doing something (using stripslashes) to do some cleaning. I don't know what you're wanting to clean, but this is at least something.
- the clean function will now process the data go_clean returns, putting it all into one big merged array
- the clean function returns the merged array

Now, it's horribly overcomplex code (there's no need for two functions), but hopefully it's a nice example .

What the "&" does by the way is a bit complex. Basically a normal function treats variables passed into it as new data, and the function could change that data and not affect the data that was passed into the function. If "&" is put before the parameter names then PHP will bind the passed data to the data in the function, so that if you make a change in the function, the actual passed source data will also be changed.
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:09 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03240 seconds
  • Memory Usage 2,177KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (2)post_thanks_box
  • (2)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (2)post_thanks_postbit_info
  • (2)postbit
  • (2)postbit_onlinestatus
  • (2)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete