DEV Community

Cover image for Simply way to write PHP output in Javascript console.log
Wallace Maxters
Wallace Maxters

Posted on • Originally published at wallacemaxters.com.br

2 2

Simply way to write PHP output in Javascript console.log

You can simply combine the printf and json_encode function to make this.

function console_log($data) {
    printf('<script>console.log(%s);</script>', json_encode($data));
}

console_log(['name' => 'Wallace Maxters']);
Enter fullscreen mode Exit fullscreen mode

Output:

image

Latest comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay