SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    This is Driving me Nuts! HaHa

    Hi,

    I sincerely apologise for all my threads.
    This is driving me nuts.

    I've completed my entire script. However, I'm faced with this problem.

    I have several links of code which is re-used.
    This code consists of while statements..variable forming...several if/elseif/else statements and many more.

    I owuld like to be able to refer to this code from an external file, and also run statements to modify some of the lines. Like if ($variable =='1') { hide this line }
    else { show this line }

    etc. Can anyone help me?

    What do you do to optimise your code? Are there any tutorials on that?

    I'm afraid I can't refer to the php manual and it doens't answer my queries.

    Once again, my apologies in taking up your time.
    Thank You
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well you've kind of given yourself the answer in your question. You have to code a control structure to handle branched logice; ie an if/else. For example you could do this:
    PHP Code:
    ------------
    include_file.com
    ------------
    <h1> Here are the links</h1>
    <?php
    if ($foo == '1') {
       echo 
    '<a href="foo.html\">foo</a><br />';
    } else {
       echo  
    '<a href="bar.html">bar</a><br />';
    }
    ?>
    And more html goes here....

    ------------
    main_script.php
    ------------
    <?php
    $foo 
    0;
    include(
    "include_file.inc");
    ?>
    Or else you wrap up the bit of your code that contains the branched logic into a function and pass it a flag to tell it what to do.
    PHP Code:
    function printLink($flag) {
       if (
    $flag == '1') {
          
    // print something to the browser
       
    } else {
          
    // print something else
       

    BTW, all non-zero values evaluate to TRUE and zero evaluates to FALSE. You can also use the keywords true and false to assign a boolean value. In other words you can do this:
    PHP Code:
    $flag true;

    if (
    $flag) {
       
    // flag is true so this will be executed
    } else {
       
    // had flag been false this would have been executed

    Had I said $flag = 1 then the above code would have executed the same because 1 evaluates to true so $flag evaluates to true.

  3. #3
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    BTW..freakysid. are you online in icq now? =)
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  4. #4
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright. How about when I'm using functions..

    say the code below is going to be re-used.

    PHP Code:
    if ($userfile != 'none') {
    // Gets File Extension for File (Category)
    $tmp explode("."$userfile_name);
    $ext = (count($tmp) > 2) ? $tmp[count($tmp) - 1] : $tmp[1] ;

    $userfile_name str_replace(' '"_"$userfile_name); // Replace all Spaces with a Underscore

    if ($ext == 'zip') { $filetype_error=0; }
    elseif (
    $ext == 'exe') { $filetype_error=0; }
    else { 
    $filetype_error=1; }

    if (
    $filetype_error) { $errors[] = "Invalid FileType [$ext]."$errors[] = "File Extension must be in Lowercase"; }
    else {
        if (
    file_exists("$path/$userfile_name") ) { $errors[] = 'File Already Exists'; }
        else {
            if (!
    ereg("^[-_[:alnum:]\.]+$"$userfile_name)) {
                
    $errors[] = 'Filename Cannot Contain anything but Letters, Numbers, Underscores and Dashes.';
            }
        }

    should my function be something like:

    PHP Code:
    function checkFileUpload ($userfile$userfile_name) {
            if (
    $userfile != 'none') {
            
    // Gets File Extension for File (Category)
            
    $tmp explode("."$userfile_name);
            
    $ext = (count($tmp) > 2) ? $tmp[count($tmp) - 1] : $tmp[1] ;

            
    $userfile_name str_replace(' '"_"$userfile_name); // Replace all Spaces with a Underscore

            
    if ($ext == 'zip') { $filetype_error=0; }
            elseif (
    $ext == 'exe') { $filetype_error=0; }
            else { 
    $filetype_error=1; }

            if (
    $filetype_error) { $errors[] = "Invalid FileType [$ext]."$errors[] = "File Extension must be in Lowercase"; }
            else {
                    if (
    file_exists("$path/$userfile_name") ) { $errors[] = 'File Already Exists'; }
                    else {
                            if (!
    ereg("^[-_[:alnum:]\.]+$"$userfile_name)) {
                                    
    $errors[] = 'Filename Cannot Contain anything but Letters, Numbers, Underscores and Dashes.';
                            }
                    }
            }

    and called like this.

    checkFileUpload($userfile,$userfile_name);
    checkFileUpload($userfile2,$userfile2_name);
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  5. #5
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Lyon, its almost 2AM here and I am going to bed!

    However, the idea of your code looks good. However, the problem is that if the array $errors has a scope specific to the function and wil be created inside the function and destroyed once the function exits. If there is another $errors array outside the scope of the function it does not exist inside the scope of the function.

    So you need to do either of three things:

    1) If the array $errors does not exist outside the scope of the function prior to entering the function then you can simple return the array. That is add a line to the function code
    PHP Code:
    function*checkFileUpload*($userfile,*$userfile_name)*{
       
    // blah, blah, blah
       
    return $errors;

    Then you would call the function like so:
    $errors = checkFileUpload($userfile,$userfile_name);

    2) If upon calling the function from your main script an array $errors already exists in the scope of your main script then you can declare it as global in your function
    PHP Code:
    function*checkFileUpload*($userfile,*$userfile_name)*{
       global 
    $errors;
       
    // blah, blah ,blah

    3) The most correct (but more of a pain in the bum) method is to pass a reference to the array when calling the function. Passing a reference is different to passing a value to a function. When we pass a value we are passing a copy. For example, in your signature for function checkFileUpload you pass $userfile by value. If you go messing about with the value of $userfile inside the scope of the function it is not going to change the value outside the scope of the function. But when you pass a variable by reference you are handing over control of that variable to the function. Any change you make to the variable inside the function will change the variable in in global scope too (just like in option 2 above).

    Eg
    PHP Code:
    function*checkFileUpload*($userfile,*$userfile_name, &$errors)*{
       
    // any changes you make to errors inside this function are in effect
       // are changing the variable in global scop (not a local copy).

    Note the & prefix on $errors tells php to treat $errors as a reference (a handle) to the variable which is in global scope and not to make a copy in local scope as is done for $userfil and $userfil_name.

    I don't know if that is of help. I'm ranting now. To bed with me. I'll have to recompile my mod_php build of php in the morining

  6. #6
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Freakysid,

    I'll try your 2nd method. I don't quite understand the 3rd. So by using the 3rd method, i don't need to declare the global and the return?

    Thanks

    OH> But you never come online in icq?
    its 12 am here too. heehz...2 hours before you.
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  7. #7
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Reffering to topic...

    I like pecans....
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"

  8. #8
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Reffering to topic...

    Originally posted by New Oddity
    I like pecans....
    lol. you are corny or what. hahah!
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  9. #9
    SitePoint Zealot New Oddity's Avatar
    Join Date
    Jun 2001
    Location
    Georgia
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Corny no... Nutty yes...

    Pastachios are good too...
    --Odd
    "We all live in a yellow subroutine."
    "Some call it insanity; I call it inspiration!"


Bookmarks

Posting Permissions

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