Lesson 12 - Strings as Parameters in PHP

A string can be passed as parameter to a method. Parameters must be declared within the class. In order to further understand this topic, let us analyze the following portion of PHP code:



<?php

class userProfile{       //method 

   public function displayProfile($name,$gender,$occupation){

               echo "Name:$name";

        echo "<br/>"; //line break using HTML 
        echo "Gender:$gender";

               echo "<br/>";  //line break using HTML

               echo "Occupation:$occupation";

                

               }

    

} 

//instantiating object

$object=new userProfile(); 
$object->displayProfile("Raymundo Cerón","male","programmer"); 
?>  


In the previous PHP code a class named "userProfile" was declared, such a class contains a method called "displayProfile" that receives three parameters. The first parameter is "$name", the second parameter is "$gender" and the last parameter is "$occupation". All of them are strings. As shown above the parameters are defined within  the parentheses, a dolar sign is set next to the name of the parameter and the parameters are separeted by commas. Finally the method "displayProfile" takes all the parameters and prints them out. 

By executing the previous PHP script  the resulting outcome is:

Name:Raymundo Cerón
Gender:male
Occupation:programmer

No comments:

Post a Comment