• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / php json_encode() Function – Get JSON Representation of Variables in php

php json_encode() Function – Get JSON Representation of Variables in php

March 31, 2022 Leave a Comment

The php json_encode() function allows us to get the JSON representation of variables.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

$num = 5;
$string = "This is a string";
$array = ["This","is","an","array"];

echo json_encode(new exampleClass) . "\n";
echo json_encode($num). "\n";
echo json_encode($string). "\n";
echo json_encode($array). "\n";

//Output:
{"id":null}
5
"This is a string"
["This","is","an","array"]

When working with variables, the ability to easily convert a variable to JSON and get the JSON representation of a variable is very useful.

The php json_encode() function returns the JSON representation of a variable in a string. json_encode() is similar to the JavaScript stringify() function.

We can use json_encode() to convert strings, ints, floats, arrays and objects to JSON.

Below are some examples of using json_encode() to convert different variables to JSON in php.

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

$num = 5;
$string = "This is a string";
$array = ["This","is","an","array"];

echo json_encode(new exampleClass) . "\n";
echo json_encode($num). "\n";
echo json_encode($string). "\n";
echo json_encode($array). "\n";

//Output:
{"id":null}
5
"This is a string"
["This","is","an","array"]

If you want to change how encoding happens, there are many JSON constants available for you to use.

Printing Arrays in php with json_encode() Function

We can print an array in php is with the help of the php json_encode() function.

If you have an array, you can pass it to json_encode() and get the JSON representation of the array.

Below is an example in php showing how to print an array with the help of json_encode().

$array = ["This","is","an","array"];

echo json_encode($array);

//Output:
["This","is","an","array"]

Printing Objects in php with json_encode() Function

If you have an object, you can pass it to print_r() and get a JSON representation of the object. json_encode() will show the public variables, but not private variables of the object.

Below is an example in php showing how to print an object with json_encode().

class exampleClass {
    public $id;
    private $name;
    static protected $multiplyNumbers;

    static function multiplyNumbers($a,$b) {
        return $a * $b;
    }
}

echo json_encode(new exampleClass);

//Output:
{"id":null}

Hopefully this article has been useful for you to learn how to use json_encode() in your php code.

Other Articles You'll Also Like:

  • 1.  php var_dump() Function – Print Information about Variable Type and Value
  • 2.  php array_splice() – Remove, Insert, and Replace Elements in Array
  • 3.  php Random Number Generator with rand Function
  • 4.  php Convert Radians to Degrees with php rad2deg() Function
  • 5.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 6.  php rtrim() Function – Remove Whitespace at End of String
  • 7.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 8.  Check if String Ends with Given String in php with str_ends_with()
  • 9.  Add Months to Date in php
  • 10.  Get Word Count of String in php with str_word_count() Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy