Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 30

Thread: Vars

  1. #1
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Vars

    hi,

    im wandering the dis-advantage AND advantages of variables, I see alot of pro scripts use:
    Code:
    $config['site_url']       = 'mysite.com';
    $config['site_name']       = 'mysite.com';
    $config['site_desc']       = 'mysite.com';
    ^^^ I know they're array, btw wwhy use that as opposed to:t
    [code]
    $site_url
    $site_name
    $site_desc
    [/code=]

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    IMO Its easier to use collections than individual variables, especially if passing between functions.
    PHP Code:
    function doStuff(array $config)
    {
        
    print_r($config);
    }

    function 
    doStuff()
    {
        
    $argv func_get_args();
        
    print_r($argv);

    The other pro is that you can reference the array while you cannot late bind a reference on a variable parameter list.

    In any case, it doesn't matter if you use variable, arrays or constants to store data. Its all a matter of how you use it.

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,030
    Thanks
    15
    Thanked 240 Times in 240 Posts
    Because site_url is something that needs to stay fixed throughout the session, so they choose to represent it as a constant and not a variable, $site_url. After all site_url is used to identify something and needs to never change it's meaning.

    Changes: I get what you mean, I thought you meant use $site_url inside the $config brackets. One really good reason is when you use _SESSION you don't have to put everything as a session variable, you can just pass a reference to the $config array instead by using _SESSION.
    Last edited by DrDOS; 07-14-2012 at 12:40 AM.

  • #4
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    Ok, lets see if i understand it.

    I'v got config.php -
    PHP Code:
    $config['site_address']                            = 'http://www.thishosting.org'// No slash
    $config['site_path']                            = $_SERVER['DOCUMENT_ROOT'];
    $config['site_email']                            = 'i293';

    $config['upload_destination']                    = 'webmaster@domain.com';
    $config['thumb_destination']                    = 'thumbs';

    $config['site_name']                            = 'ImgIze Script';
    $config['site_desc']                            = 'i293';

    $config['max_width']                            = 1900;
    $config['max_height']                            = 1900
    I need to use some ^^ in a function:
    PHP Code:
    $aConfig $_SESSION['DOCUMENT_ROOT'] .'/config.php';

    function 
    test(array $aConfig) {
       
    // I can use em now?


  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Uh... no.

    $aConfig is a string, not an array.

    If you want to pass $aConfig, you don't need to put array before it, specially since its not an array. Simply function text ($aConfig) { will work, as long as when you call the function, you pass it $aConfig. However, if you're going to use the variable in there, you can also make it global within the function

    PHP Code:
    function text () {
    global 
    $aConfig;

    Now $aConfig is the same version that is outside the function. However, as mentioned, as these are configs that are not going to change during runtime, set them to constants rather then as variables. Constants are always global.

  • #6
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    can u show me a way by constants..............

    so to pass it:
    PHP Code:
    func($aConfig); 
    then to use:
    PHP Code:
    function func($aConfig) {} 

  • #7
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    well thats nnot working:

    func.php -
    PHP Code:
    function performaLocalUpload($files$aConfig) {
        global 
    $aConfig;
        if(
    $ext checkitsaValidImage($files))
        {
            
    $new_name gen_uniqueFilename() . $ext// we will give an unique name.
            
    $uploadFilePath $aConfig['upload_destination'] .'/'$new_name;

            if (!
    move_uploaded_file($files$uploadFilePath)) {
                
    trigger_error('Failed to upload image');
            }
            
            
    // Create the thumbnail
    #        create_thumb($customPixels, $save_image, $new_name, $type, $thumbDir);
        
    }

    uploader.php -
    PHP Code:
    t was writtn by Christopher ReynoldsFeb 2011 **************************************/
    $aConfig $_SERVER['DOCUMENT_ROOT'] .'/config.php';

    if(isset(
    $_FILES['image_upload']) && !empty($_FILES['image_upload'])) {
        
    // Local upload
        
    $tFiles $_FILES['image_upload']['tmp_name'];
        
    $aFiles $_FILES['image_upload'];
        
        for(
    $i 0$i count($tFiles); ++$i)
        {
            
    performaLocalUpload($aFiles['tmp_name'][$i], $aConfig);
        }


  • #8
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    so why's Fou-Lu told in post #2, I can do
    Code:
    array $config

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I wouldn't recommend global unless you cannot modify the signature.

    You can use array $config as a part of the parameter list *if* you provide it with an array. You have not.
    PHP Code:
    $aConfig $_SESSION['DOCUMENT_ROOT'] .'/config.php';

    function 
    test(array $aConfig) {
       
    // I can use em now?
    }  

    test($aConfig); // with throw an error. 
    $aConfig is a string, not an array.

  • #10
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    so then in the funci can do config's like:

    PHP Code:
    $aConfig['xxxxxxxxxxxxx'
    and how would i call the array?
    Last edited by Chris-2k; 07-26-2012 at 02:03 PM.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by Chris-2k View Post
    so then in the funci can do config's like:

    PHP Code:
    $aConfig['xxxxxxxxxxxxx'
    and how would i call the array?
    You mean into a function? You pass it $aConfig.

  • #12
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    done all that mate, now getting:


    Catchable fatal error: Argument 2 passed to performaLocalUpload() must be an array, string given

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Then your provided parameter is not an array.

  • #14
    Regular Coder
    Join Date
    May 2011
    Posts
    303
    Thanks
    29
    Thanked 0 Times in 0 Posts
    what you mean?

    To call;
    PHP Code:
    test($aConfig
    then:
    PHP Code:
    fnction(array $aConfig)
    {
     
    $aConfg['xxxxxxxx']


  • #15
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I don't understand your question.
    If the function parameter list is typecast as an array or object class, then you need to provide it with that type in order to process the function. If it doesn't receive that type, it throws an E_RECOVERABLE_ERROR. This saves a check against is_array or instanceof within the function.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •