How to find the area of ​​a rectangle in PHP

藏色散人
Release: 2023-02-24 10:36:01
Original
7898 people have browsed it

How to find the area of ​​a rectangle in PHP

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;?
>
Copy after login

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;
}
Copy after login

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 &#39;矩形长是&#39;.$this->length."<br />";
    echo &#39;矩形宽是&#39;.$this->width."<br />";
    echo &#39;矩形面积是&#39;.$this->length*$this->width."<br />";
}
Copy after login

}-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);
Copy after login

We require the area, just Call the getmessage method just used to find the area. Then add the following code:

$rectangle1->getmessage();
Copy after login

Then we save it and then run it and view the results:

How to find the area of ​​a rectangle in PHP

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!