Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction🥸
2.
What are Variables?
2.1.
Example
2.2.
Explanation
2.3.
Output
3.
What is an Array?
3.1.
Example
3.1.1.
Simple Array
3.1.2.
Multidimensional Array
3.1.3.
Associative Array
4.
What is print_r() Function?
4.1.
Syntax
4.2.
Explanation
4.3.
Output
5.
What is the var_dump() Function?
5.1.
Syntax
5.2.
Explanation
5.3.
Output
6.
Frequently Asked Questions
6.1.
What is PHP?
6.2.
What is the print_() Keyword?
6.3.
What is the var_dump() Keyword?
6.4.
What are Variables?
6.5.
What is an Array?
7.
Conclusion
Last Updated: Mar 27, 2024
Medium

How to Print Array in PHP?

Introduction🥸

PHP is an acronym for "Hypertext preprocessor" Earlier, PHP was abbreviated as a personal home page. It is a programming language used to design web applications and websites. It is a server-side scripting language that embeds HTML to develop a dynamic website, static website, or web application. Rasmus Lerdorf created PHP in 1994. The syntax of PHP is similar to C++, C, and Java. Php manages dynamic website database, content, session, cookies, etc.

How to print array in PHP?

We will go through one problem of how to print array in PHP in this blog, but before that, we will learn what print_r(), var_dump(), variables, and array are.

What are Variables?

What are Variables?

We use the sign  for declaring a variable in PHP, followed by the variable's name.

Example

<?php
$mesgForNinjas = "Hello Ninjas";
$noOfNinjas = 20;
?>
You can also try this code with Online PHP Compiler
Run Code

Explanation

After running the above-given program, the variable $mesgForNinjas will hold the value "Hello Ninjas," and the variable $noOfNinjas will hold the value 20. 

Note- While assigning a string value to a variable, use double/ single quotes.

 

A variable can have a descriptive or short name. Some of the rules for declaring variables are given below-

1️⃣The sign  for declaring a variable should be used, followed by the variable's name.

2️⃣Variable names can start from an underscore or letter.

3️⃣Variable names can not start with a number and can only contain alpha-numeric characters and underscore.

4️⃣The name of arable in PHP is case sensitive, which means $name and $NAME are two different variables.

For printing the variable, you can use the echo keyword.

<?php
$mesgForNinjas = "Hello Ninjas!";
$noOfNinjas = 20;
echo $mesgForNinjas;
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Hello Ninjas!

 

Cool! We hope you understand what variables are. Let’s move further and see what arrays are and how we define them in PHP. And go through the procedure of how to print array in PHP.

What is an Array?

What is an Array?

An array in PHP is a special variable that can simultaneously store more than one value. The array()  function is used to create an array in PHP. Three types of arrays are there in PHP, and that is 

  • Indexed arrays - Arrays where values have a numeric index
  • Multidimensional arrays - Where the array contains one or more arrays
  • Associative arrays - Arrays where values have named keys

Example

In this section, we will see different types of arrays which can be declared in PHP.

Simple Array

$subjects = array("0"=>"maths","1"=> "english","2"=> "science","3"=> "hindi"); /* here index are numeric starting from 0*/
You can also try this code with Online PHP Compiler
Run Code

 

The output will look something like this -

output

Multidimensional Array

$subjects = array("0"=>"maths","1"=> "english","2"=> array("physics","chemistry", "biology"),"3"=> "hindi"); /* here the key 2 has value as array */
You can also try this code with Online PHP Compiler
Run Code

 

The output will look something like this -

output

Note - When you don't define any key to the values of the array, by default, the key is numeric and starts from 0.

Associative Array

$subjects = array("one"=>"maths","two"=> "english","science"=> array("physics","chemistry", "biology"),"four"=> "hindi"); /* the keys in this example are strings/ named */
You can also try this code with Online PHP Compiler
Run Code

 

The output will look something like this -

output

For now, you don't have to worry about printing an array. Because soon we will learn how to print array in PHP.

Are you clear with the concept of arrays in PHP? Nice! Let’s move further and learn how we can print array in PHP.

What is print_r() Function?

What is print_r() Function?

Using the print_r function, we can print human-readable information about a variable. In other words, PHP's print_r() function is used to print the value stored in a variable. We can also use this function to print an array. This function prints all the values of the given array with their index number.

Syntax

print_r($varName, $boolVar)

Explanation

The print_r() of PHP has two parameters, as you can see above. The first parameter, $varName, is required, and the mandatory parameter as its value will be printed. The second parameter, $boolVar, is optional. The value of the second parameter is false by default and is used to store the output of the print_r()  function. If you enter the value of $boolVar as true, it will return the value which s to be returned. 

<?php
/* Here we are declaring an array */
$subjects = array("maths", "english", "science", "hindi");
/*  now we are printing the array using print_r() function */
var_dump($subjects);
?>
You can also try this code with Online PHP Compiler
Run Code

Output

Array
(
    [0] => maths
    [1] => english
    [2] => science
    [3] => hindi
)

 

Good Job! You have come so far and learned one method to print an array in PHP. Now we'll see one more method to print array in PHP.

What is the var_dump() Function?

What is the var_dump() Function?

The second function we use to print array is var_dump(). Let’s learn about this function in this section.

For printing the details of any variable or expression, we use the var_dump() function. This function also prints the array’s values with its index value, the data type of each element, and the length of each element. This function provides structured information about an array or variable.

Syntax

var_dump($varName)

Explanation

The var_dump() only takes one parameter, $varName, and returns the variable’s structured information.

<?php
/* Here we are declaring an array */
$subjects = array("maths", "english", "science", "hindi");
/*  now we are printing the array using print_r() function */
var_dump($subjects);
?>
You can also try this code with Online PHP Compiler
Run Code

Output

array(4) (
  [0]=>
  string(5) "maths"
  [1]=>
  string(7) "english"
  [2]=>
  string(7) "science"
  [3]=>
  string() "hindi"
)

 

We hope you have understood everything about how to print array in PHP. 🙌🥳
Check out this problem - Maximum Product Subarray

Frequently Asked Questions

What is PHP?

PHP is an acronym for "Hypertext preprocessor." Earlier, PHP was abbreviated as a personal home page.

What is the print_() Keyword?

PHP's print_r() function prints the value stored in a variable. We can also use this function to print an array. 

What is the var_dump() Keyword?

For printing the details of any variable or expression, we use the var_dump() function. This function also prints the array’s values with its index value, the data type of each element, and the length of each element. 

What are Variables?

Variables are used to store data. In PHP, the sign  for declaring a variable is followed by the variable's name.

What is an Array?

An array in PHP is a special variable that can simultaneously store more than one value. The array()  function is used to create an array in PHP. 

Conclusion

In this blog, we saw how to print array in PHP, but before that, we will learn what print_r(), var_dump(), variables, and array are and if you would like to learn more, check out our articles on

Recommended problems -

 

Refer to our guided paths on Coding Ninjas Studio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. Enrol in our courses and refer to the mock test and problems available. Take a look at the interview experiences and interview bundle for placement preparations.

Happy Learning Ninja! 🥷

Live masterclass