[RESOLVED] Returning true
Results 1 to 3 of 3

Thread: [RESOLVED] Returning true

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487

    resolved [RESOLVED] Returning true

    I'm not sure how to do this one

    pbismad had mentioned this in another thread
    5) Your methods need to return a TRUE value when they succeed. They are currently returning a NULL value upon success, which your current code will treat the same as a FALSE value.
    PHP Code:
    class Login
    {    
        private 
        
    $email,
        
    $password;


        public function 
    validEmail($email)
        {    
            return (
    filter_var($emailFILTER_VALIDATE_EMAIL) !== FALSE);  

            return 
    $email;
        }    

        public function 
    emptyPassword($password)
        {
            return (empty(
    $password) !== TRUE);

            return 
    $email;
        }
        
        

    How do I return it as true? Recommended reading would be appreciated initally I had thought something like this...

    PHP Code:
    class Login
    {    
        private 
        
    $email,
        
    $password;


        public function 
    validEmail($email)
        {    
            return (
    filter_var($emailFILTER_VALIDATE_EMAIL) !== FALSE);  

            return 
    $email;
                    return 
    true;
        }    

        

    But that seems too simple to be right
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  2. #2
    Senior Member
    Join Date
    Apr 2016
    Posts
    127
    You can only have one return statement within any code execution path, because the first one that gets executed stops execution along that path, so all the above code makes no sense.

    My comment referred to code that was returning a false value upon failure, but wasn't doing anything upon success. In this case, when the code in the function/method reached the end of execution, a null value is returned to the calling code. If I remember correctly, the code testing the returned value was only doing a 'loose' comparison (an == or a != ). For these comparisons, a false and a null are the same, so, for that particular code, doing nothing for a success, which returned a null value, would look exactly the same as a false value.

    The 'fix' for that particular code would have been to explicitly return a true value upon success (a null value would not be a good choice for a success value.)
    Programming should not be a painful activity. If you are experiencing pain while programming, you are probably doing something wrong.

  3. #3
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487
    Ah ok, I'm with you. I have some new tutorial website I'm assured is good so we'll see how it goes
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

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
  •