search
HomeBackend DevelopmentPHP ProblemHow to output php array object

How to output php array object

Apr 27, 2023 am 09:08 AM

In the PHP language, arrays and objects are very important data types. At the same time, in many cases, we need to output the array or object for other programs or people to view. Therefore, this article will explain how to output PHP arrays and objects.

1. Output array

The array in PHP is an ordered set of data, which can be represented by a pair of square brackets. Arrays can contain any type of data, such as strings, numbers, and other arrays. In PHP, we can use echo or print_r function to output an array.

1. Use echo to output an array

When we use the echo function to output an array, PHP will convert the array into a string and display it on the screen. But this method does not show the structure of the array very well, because it just connects the elements of the array together in a certain order and outputs it. Therefore, in actual development, we will not often use this method to output arrays.

The following code is an example of using echo to output an array:

<?php   $arr = array(&#39;苹果&#39;, &#39;香蕉&#39;, &#39;橘子&#39;);
  echo $arr;
?>

Output result:

Array

2. Use print_r to output an array

print_r is provided by PHP A very practical function that can print out the structure information of an array and output it to the screen. Using the print_r function, we can clearly display the structure of the array and help us better understand the elements in the array. The following is an example of using the print_r function to output an array:

<?php   $arr = array(&#39;苹果&#39;, &#39;香蕉&#39;, &#39;橘子&#39;);
  print_r($arr);
?>

Output results:

Array
(
    [0] => 苹果
    [1] => 香蕉
    [2] => 橘子
)

When outputting, the print_r function will output according to the structure of the array and display the keys and values ​​separately. In this example, we can see that the array has three elements, the key of each element is a number and the value is the corresponding string.

2. Output object

Similar to arrays, objects in PHP are also very common data types. An object is an instantiation of a class and has properties and methods. In PHP, we can use echo or print_r function to output objects.

1. Use echo to output an object

When we use echo to output an object, PHP will convert the object into a string and display it on the screen. However, the output is not intuitive enough because it only shows the memory address of the object.

The following is an example of using echo to output an object:

<?php   class person {
    public $name = &#39;张三&#39;;
    public $age = &#39;18&#39;;
  }
  $p = new person();
  echo $p;
?>

Output result:

Catchable fatal error: Object of class person could not be converted to string

In this example, we try to use echo to output a person object, but the result is an error message. This is because the echo function does not support direct output objects.

2. Use print_r to output objects

Similar to arrays, we can use the print_r function to output objects. Similarly, when we pass the object to the print_r function for output, PHP will also output the structure information of the object.

The following is an example of using print_r to output an object:

<?php   class person {
    public $name = &#39;张三&#39;;
    public $age = &#39;18&#39;;
  }
  $p = new person();
  print_r($p);
?>

Output result:

person Object
(
    [name] => 张三
    [age] => 18
)

In this example, we can see that the print_r function outputs according to the structure of the object, And the properties and corresponding values ​​of the object are presented one by one. This can help us understand the structure and properties of objects very well.

3. Conclusion

In PHP, we can use the echo and print_r methods to output arrays and objects. However, since the echo function cannot display the structure of arrays and objects well, we usually use the print_r function for output. Whether it is an array or an object, using the print_r function can clearly display their structural information and help us better understand their elements and attributes.

The above is the detailed content of How to output php array object. 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor