• 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
  • Ruby
  • 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 Rename File – Renaming Files and Directories Easily
  • 2.  What is the Difference Between unset() and unlink() in php?
  • 3.  PHP Numerically Indexed Array Begin With Position 0
  • 4.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 5.  Using strtolower() in php to Convert String to Lowercase
  • 6.  How to Create an Empty Array in php
  • 7.  php Absolute Value with abs Function
  • 8.  php is_array() Function – Check if Variable is Array in php
  • 9.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 10.  How to Parse URLs with php parse_url() 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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy