Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

PART 3: PHP Questions and Answers Look at the PHP class below. Write and test PH

ID: 3785159 • Letter: P

Question

PART 3: PHP Questions and Answers

Look at the PHP class below. Write and test PHP code that instantiates the class person to make a person object. Test your code and paste it in the table cell below the class.

class person {

            var $name;

            function set_name($new_name) {

                        $this->name = $new_name;

            }

            function get_name() {

                        return $this->name;

            }

}

[ANSWER CODE HERE]

Look at the PHP code below. Which argument is a default argument ($arg1 or $arg2) and how do you know?

function foo($arg1, $arg2 = 5) {

            echo "arg1: " . $arg1 . "</br>";

            echo "arg2: " . $arg2 . "</br>";

}

[ANSWER CODE HERE]

Look at the PHP code below. What types of arguments are $arg1 & $arg2.

function foo($arg1 = '' , $arg2 = '' ) {

            echo "arg1: $arg1 </br>";

            echo "arg2: $arg2 </br>";

}

[ANSWER CODE HERE]

class person {

            var $name;

            function set_name($new_name) {

                        $this->name = $new_name;

            }

            function get_name() {

                        return $this->name;

            }

}

[ANSWER CODE HERE]

Explanation / Answer

PART1

class person {

            var $name;

            function set_name($new_name) {

                        $this->name = $new_name;

            }

            function get_name() {

                        return $this->name;

            }

}

# This was your class person and now I will show you how to instatiate an object

$new_obj1 = new person();

# This is how we instantiate the class person and now i'll show you how to call the method in the class person

$new_obj1 -> set_name("CheggStudy"):

# Upto this point "CheggStudy" string has been assigned to variable 'name'

$new_obj1 -> get_name()

----------------------------------------------------------------------------------------------------------------------------------------------------

PART 2

function foo($arg1, $arg2 = 5) {

            echo "arg1: " . $arg1 . "</br>";

            echo "arg2: " . $arg2 . "</br>";

}

Here $arg2 is a default argument because we have set a default value to it as $arg2=5. It works like this:

Suppose you mention the value of $arg1 and $arg2 while calling the function. After execution the program will take the passed values of the function.

Code Snippet:

foo(3,8);

OUTPUT:

arg1 : 3

arg2 : 8

But suppose you do not or forget to pass the value of arg2 and only pass the value of arg1 while calling the function the default value of arg2 will be considered i.e. 5.

CODE SNIPPET:

foo(2);

OUTPUT:

arg1 : 2

arg2 : 5

Here the important thing to remember is that if you do not pass the value of a argument that is not default the program will terminate immediately.

-----------------------------------------------------------------------------------------------------------------------------------------------------

PART 3:

function foo($arg1 = '' , $arg2 = '' ) {

            echo "arg1: $arg1 </br>";

            echo "arg2: $arg2 </br>";

}

arg1 and arg2 are arguments that forces to take it string values during the calling of the function. So we can safely say that arg1 and arg2 here behave like default arguments.