How to find the area of a rectangle in PHP
We first open it with Notepad, and then create a class, here It is a rectangle class, and the English word is rectangle. Then we define the method according to the class and write the code like this:
<?php class Rectangle{ public $length; public $width;? >
and then save it. This defines a class, the rectangle class.
Then we write the constructor method and initialize the rectangle class. Continue writing code:
function __construct($length,$width){ $this->length=$length; $this->width=$width; }
The keyword function used here means method. Note that there are two underscores in front of construct [in English]. There are two parameters, one is the length of the rectangle, and the other is the width.
Then we write the method to find the area,
function getmessage(){ echo '矩形长是'.$this->length."<br />"; echo '矩形宽是'.$this->width."<br />"; echo '矩形面积是'.$this->length*$this->width."<br />"; }
}-This right brace is the end of the previous class definition.
Then we use the new keyword to create a new object. The object here is a rectangle, initialized to length 10 and width 5. Then add the code as follows:
$rectangle1=new Rectangle(10,5);
We require the area, just Call the getmessage method just used to find the area. Then add the following code:
$rectangle1->getmessage();
Then we save it and then run it and view the results:
Pay attention to the orientation Object method class definition and method invocation.
For more PHP knowledge, please visit PHP tutorial!
The above is the detailed content of How to find the area of a rectangle in PHP. For more information, please follow other related articles on the PHP Chinese website!