www.webdeveloper.com
Results 1 to 2 of 2

Thread: Getting variables from functions

  1. #1
    Join Date
    May 2010
    Posts
    13

    Question Getting variables from functions

    Hello,

    I am still trying fully grasp functions in php. I wrote a script that picks a random number between 1 and whatever number is entered when calling the function. I can echo the number it creates in the its function and then I call it to get the number echoed. I am trying to randomly display an image from a folder. But I can not call the variable and its really weighing me down because I want get the images size with getimagesize. I have tried global but its not working. Any help would be great.

    Thanks so much

    Here is what I have and tried commented out:
    PHP Code:
    function rand_uni($high){

    $num_to_convert=rand(1,$high);


    if(
    $num_to_convert<10){
        
    $rand_num_out="000" $num_to_convert ;    
    }else if(
    $num_to_convert<100){
        
    $rand_num_out="00" $num_to_convert ;    
    }else if(
    $num_to_convert<1000){
        
    $rand_num_out="0" $num_to_convert ;    
    }else{
        
    $rand_num_out$num_to_convert;    
    }

    echo 
    $rand_num_out;

    //global $rand_num_out;
    }


                                                          
    //which is called in //

    function obj(){
        
        
    $obj_height "100";
        
    $obj_width "150";


    $obj_directory "_img/_objects/";
    if (
    glob("$obj_directory*.gif") != false)
    {
     
    $obj_filecount count(glob("$obj_directory*.gif")); 

    //rand_uni($obj_filecount);
    //global $rand_num_out;


                               //How I am Calling it now, wish I could just call the variable//
     
     
    echo"<img src=\"" $obj_directory;
     
     
    rand_uni($obj_filecount);
     
     echo 
    ".gif\">";


     
    }else{echo 
    "no objects found :(";}
                                     

    Last edited by Savour; 12-03-2010 at 09:47 PM. Reason: clarification

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,925
    Generally, you want to return the value of interest from the function.
    PHP Code:
    // a stupid example function
    function foo($value)
    {
       return 
    $value 2;
    }

    echo  
    foo(3); // outputs "6" 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles