WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] need help in understanding one line code (3 posts)

  1. imjscn
    Member
    Posted 3 years ago #

    I need to remove the function save(),
    but this function is used by another function data_remove(),

    I don't understand this line:
    return ( $save === true ) ? $this->save() : true;

    If I removed save() function, how can I modify this line to keep the data_remove() working?

    The 2 functions currently working in a plugin as following:

    function data_remove( $save = false ) {
            $this->_data_obj = null;
            return ( $save === true ) ? $this->save() : true;
        }
    
        function save(){
            // some codes here to save all data into database table
            return true;
        }
  2. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    return ( $save === true ) ? $this->save() : true;

    the above is a ternary operator:

    http://php.net/manual/en/language.operators.comparison.php
    scroll down to:

    Ternary Operator
    Another conditional operator is the "?:" (or ternary) operator.

    i.e. this:
    return ( $save === true ) ? $this->save() : true;

    is the same as:

    if( $save === true ) {
    return $this->save();
    } else {
    return true;
    }
  3. imjscn
    Member
    Posted 3 years ago #

    Thanks, alchymyth! That helps a lot!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags