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.
Results 1 to 8 of 8
  1. #1
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts

    How to Initialize this...

    First off, what is this operator called...
    Code:
    .=

    Here is the code snippet causing me issues... (See Comments in Code)
    PHP Code:
        if (empty($errors)){
            
    // Valid Form Data.

            // IF I REMOVE THIS THEN I GET AN "UNDEFINED INDEX ERROR"
            // BUT IF I LEAVE IT IN, IN THE NEXT SECTION I ALWAYS GET "INVALID"
            
    $errors['pmTo'] = '';

            foreach (
    $toListArray as $toUsername){
                if (
    $toUsername is valid){
                    
    // Valid Recipient Format.

                
    }else{
                    
    // Invalid Recipient Format.
                    
    $errors['pmTo'] .= "'$toUsername' must be 8-30 characters.<br />\r\n";
                }
            }

        }else{
            
    // Invalid Form Data.
            // Drop through to display Errors.

        
    }//End of CHECK RECIPIENT-FORMAT

    ///////////////
        
    if (empty($errors)){
            
    // Valid Form Data.
            
    echo "VALID";
            exit();

        }else{
            
    // Invalid Form Data.
            
    echo "INVALID";
            exit();

        } 

    This is a Catch-22 situation, because if I don't initialize $errors['pmTo'], then the $errors['pmTo'] .= blows up.

    However if I initialize things like this...
    PHP Code:
            $errors['pmTo'] = ''
    ...then the ELSE branch of this code will never fire...
    PHP Code:
        if (empty($errors)){
            
    // Valid Form Data.
            
    echo "VALID";
            exit();

        }else{
            
    // Invalid Form Data.
            
    echo "INVALID";
            exit();

        }
    //End of CHECK RECIPIENT-FORMAT 

    I'm probably being dumb here, and missing something obvious?!

    If someone can help me to write this code in a better way, that would be appreciated!!

    Sincerely,


    Debbie

  • #2
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,912
    Thanks
    15
    Thanked 227 Times in 227 Posts
    In php .= appends a string to another string, just as += does in javascript.

    And you initialize by declaring the first string, which may be empty.

    $stk=""; or $stk="Joe ";

    $stk .= "Smith";

    The first case give the string Smith, the second gives the case Joe Smith.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #3
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by DrDOS View Post
    In php .= appends a string to another string, just as += does in javascript.

    And you initialize by declaring the first string, which may be empty.

    $stk=""; or $stk="Joe ";

    $stk .= "Smith";

    The first case give the string Smith, the second gives the case Joe Smith.
    I guess you didn't read very far into my OP...


    Debbie

  • #4
    New Coder
    Join Date
    Nov 2011
    Location
    Ratio, Logic
    Posts
    59
    Thanks
    3
    Thanked 6 Times in 6 Posts
    Concatenating assignment operator.

    Edit: possible reason for result is that $errors is not empty before first line of code here presented.
    Last edited by Tpojka; 11-28-2013 at 11:13 PM.

  • #5
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    Found a solution myself.

    Nevermind.

  • #6
    New Coder
    Join Date
    Nov 2011
    Location
    Ratio, Logic
    Posts
    59
    Thanks
    3
    Thanked 6 Times in 6 Posts
    I had no doubt about that.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,301
    Thanks
    57
    Thanked 525 Times in 512 Posts
    Blog Entries
    5
    Deb,

    I will post my answer on one condition. No back chat, disagreement etc.

    Quote Originally Posted by doubledee View Post
    First off, what is this operator called...
    PHP Code:
        if (empty($errors)){
            
    // Valid Form Data.

            // IF I REMOVE THIS THEN I GET AN "UNDEFINED INDEX ERROR"
            // BUT IF I LEAVE IT IN, IN THE NEXT SECTION I ALWAYS GET "INVALID"
            
    $errors['pmTo'] = ''
    Yes you must define this 'key' in the array otherwise it does not exist and you'll get an undefined index error - the index is also known as the key. The only way to do so is to set it to something - empty string, null, integer etc.

    Quote Originally Posted by doubledee View Post
    PHP Code:
        if (empty($errors)){
            
    // Valid Form Data.
            
    echo "VALID";
            exit();

        }else{
            
    // Invalid Form Data.
            
    echo "INVALID";
            exit();

        } 

    This is a Catch-22 situation, because if I don't initialize $errors['pmTo'], then the $errors['pmTo'] .= blows up.
    Yes it will, continue reading.

    Quote Originally Posted by doubledee View Post
    However if I initialize things like this...
    PHP Code:
            $errors['pmTo'] = ''
    ...then the ELSE branch of this code will never fire...
    PHP Code:
        if (empty($errors)){ 
    No because the $errors array is not empty. It contains one indexed item with a blank string. Instead test for the key using isset() and != '' or == '' like this;

    PHP Code:
    if ((isset($errors['pmTo'])) and ($errors['pmTo'] != ''))
       {
       
    //Do your thang
       

    Alternatively if you want a minimal code change:
    PHP Code:
    if (empty($errors['pmTo'])) 
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Senior Coder doubledee's Avatar
    Join Date
    Mar 2011
    Location
    Arizona
    Posts
    1,057
    Thanks
    25
    Thanked 0 Times in 0 Posts
    tangoforce,

    I went with something like this which seems to work okay...

    PHP Code:
        (empty($errors['pmTo']) ? $errors['pmTo'] = '' '');

        
    $errors['pmTo'] .= "The user <b>'$toUsername'</b> was not found.<br />\r\n"
    Sincerely,


    Debbie


  •  

    Posting Permissions

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