Home >Backend Development >PHP Problem >How to change all negative numbers in an array to positive numbers in PHP

How to change all negative numbers in an array to positive numbers in PHP

coldplay.xixi
coldplay.xixiOriginal
2020-08-20 10:12:003798browse

PHP method to turn all negative numbers in the array into positive numbers: first create a new php file and declare the file type and encoding for PHP to interact with the browser; then define an array containing negative numbers and define a for loop Traverse the array; then use the function [abs()] to change the elements in the array to positive numbers; finally run the web page to output the array.

How to change all negative numbers in an array to positive numbers in PHP

PHP method to change all negative numbers in the array to positive numbers:

1. Create a new 252. php

2. Enter the structure of the php web page (), as shown in the figure:

How to change all negative numbers in an array to positive numbers in PHP

3. Declare PHP and browse The file type and encoding for server interaction, as shown in the figure:

How to change all negative numbers in an array to positive numbers in PHP

#4. Define an array $arr containing negative numbers, code: $arr = [10, -9, 3, -66];

How to change all negative numbers in an array to positive numbers in PHP

5. Define a for loop to traverse the array. The code is as shown in the figure:

How to change all negative numbers in an array to positive numbers in PHP

6. Use the abs() function to change the elements in the array $arr to positive numbers, and use the print_r() function to output. The code is as shown in the figure:

How to change all negative numbers in an array to positive numbers in PHP

7, Run the web page, output the array $arr, and check whether all of them have been converted into integers, as shown in the figure:

How to change all negative numbers in an array to positive numbers in PHP

Related learning recommendations: php Programming(video)

The above is the detailed content of How to change all negative numbers in an array to positive numbers in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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