JIYIK CN >

Current Location:Home > Learning > PROGRAM > PHP >

Printing to the console in PHP

Author:JIYIK Last Updated:2025/04/12 Views:

This article explains how to write to the console in PHP.


console.log()Writing to the console using JavaScript from PHP

We can write to the console using some JavaScript from PHP. We use in JavaScript console.log()to write anything to the console. We can echoprint from JavaScript in PHP using the print statement console.log(). If we use a PHP variable as console.log()an argument to then the contents of the variable will be written to the console. We can use is_array()the print function to check if the variable to be printed is an array. If the element is an array, we can concatenate the array elements into a single string using the concatenate function with ,as a delimiter .implode()

For example, create a function write_to_console()that takes one argument $data. $dataStore the variable in a new variable $console. Use is_array()the function to check $consoleif the variable is an array. If the variable is an array, use implode()the function ,to concatenate the array elements using and as a separator. Store the value in $consolethe variable. Then, scriptinside the tag, log the variable using console.log()the statement $console. Then echoprint the JavaScript code using the statement. Outside the function, call the function with two different arguments write_to_console(). Provide a string Hello World!as the first argument and an array [1,2,3]as the second argument.

When we go to the web page and Inspect Elementcheck the console from the Options, we can see the data written to the console. This way, we can use helper functions in PHP to write to the console.

Sample code:

<?php

function write_to_console($data) {
 $console = $data;
 if (is_array($console))
 $console = implode(',', $console);

 echo "<script>console.log('Console: " . $console . "' );</script>";
}
write_to_console("Hello World!");
write_to_console([1,2,3]);

?>

Output:

Console: Hello World! 
Console: 1,2,3

console.log()Writing to the console using JavaScript and json_encode()functions in PHP

We can write to the console in PHP using json_encode()the function and JavaScript . The function converts a given associative array into a JSON object and converts an indexed array into a JSON array. We can use this function for those data items that need to be written to the console.console.log()json_ecode()

For example, create a $datafunction with an argument write_to_console(). Inside the function, apply json_encode()the function to $datathe variable and console.loglog it using . Turn the entire expression into a string and save it in $consolethe variable. Then, sprintf()print the variable using the function sprintf('<script>%s</script>', $console);and assign the expression to the variable $console. Finally, echoprint $consolethe variable using the statement. Outside the function, $datacreate an associative array on the variable and $dayscreate an indexed array on the variable. Then, call the function using these two variables as arguments write_to_console().

Associative arrays are written as JSON objects and indexed arrays are written as JSON arrays in the console. This is how we json_encode()write to the console in PHP using the function.

Sample code:

<?php
function write_to_console($data) {

 $console = 'console.log(' . json_encode($data) . ');';
 $console = sprintf('<script>%s</script>', $console);
 echo $console;
}
$data = [ 'foo' => 'bar' ];
$days = array("Sun", "Mon", "Tue");
write_to_console($data);
write_to_console($days);
?>

Output:

Object { foo: "bar" }
Array(3) [ "Sun", "Mon", "Tue" ]

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Check if a Post exists in PHP

Publish Date:2025/04/13 Views:170 Category:PHP

PHP $_POST is a super global variable that can contain key-value pairs of HTML form data submitted through the post method. We will learn different ways to check $_POST if a and contains some data in this article. These methods will use iss

PHP with Ajax

Publish Date:2025/04/13 Views:139 Category:PHP

We will use PHP and ajax by printing a simple sum of two numbers 2 and . Also, print a php array in JSON. 3 object We will also use PHP with ajax by getting the HTML formatted output from the number division in PHP. Printing simple addition

Store Div Id in PHP variable and pass it to JavaScript

Publish Date:2025/04/13 Views:51 Category:PHP

This article shows you how to div id store a in a PHP variable and pass it to JavaScript code. We will answer the following questions. What is div id ? How to div id store in a PHP variable? How to pass variables to JavaScript code? Let’s

Returns the article tag with ID from the action page

Publish Date:2025/04/13 Views:80 Category:PHP

Let's say you're in a login form and you enter the wrong information; in this case, you probably want to go back to the login page. PHP has a built-in function header() to redirect a page to a specific page. But what if the login page is at

Switching PHP versions on Ubuntu

Publish Date:2025/04/13 Views:78 Category:PHP

Different tasks may require running multiple versions of PHP. You may need to switch PHP versions by running two sites on the same server or testing older versions of code using outdated methods. We can switch PHP versions on Ubuntu using t

Resizing images in PHP

Publish Date:2025/04/13 Views:155 Category:PHP

In this tutorial article, we will discuss about resizing images in PHP. Load the image before resizing Before we can resize an image, we must first load it as an image resource in our script. This is file_get_contents() different from using

PHP upload image

Publish Date:2025/04/13 Views:61 Category:PHP

We can upload images in PHP using simple file upload operation, but first, php.ini file upload should be enabled from Files. This tutorial demonstrates how to upload images in PHP. php.ini Enable file upload from file in PHP to upload image

Creating a signature from Hash_hmac() and Sha256 in PHP

Publish Date:2025/04/13 Views:107 Category:PHP

PHP has one of the best encryption functions for data security. Hash_hmac() The encrypt function is one of the most famous encryptors. We'll show you how to use hash_hmac and sha256 encryptors to create 安全签名 one that you can store i

Updating PHP 7.x to 7.4 on CentOS

Publish Date:2025/04/13 Views:131 Category:PHP

This article shows the steps to update the PHP version from 7.x version to 7.4 in CentOS. How to Update PHP from 7.X to 7.4 in CentOS Update operating system packages. yum update -y Check your PHP version in CentOS. php -v Prints a list of

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial