How do I display print_r on different lines? - php

When I run the following code:
echo $_POST['zipcode'];
print_r($lookup->query($_POST['zipcode']));
?>
the results are concatenated on one line like so: 10952Array.
How can I get it to display on separate lines, like so:
08701
Array

You might need to add a linebreak:
echo $_POST['zipcode'] . '<br/>';
If you wish to add breaks between print_r() statements:
print_r($latitude);
echo '<br/>';
print_r($longitude);

to break line with print_r:
echo "<pre>";
print_r($lookup->query($_POST['zipcode']));
echo "</pre>";
The element will format it with any pre-existing formatting, so \n will turn into a new line, returned lines (when you press return/enter) will also turn into new lines.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/pre

If this is what your browser displays:
Array ( [locus] => MK611812 [version] => MK611812.1 [id] => 1588040742 )
And this is what you want:
Array
(
[locus] => MK611812
[version] => MK611812.1
[id] => 1588040742
)
the easy solution is to add the the <pre> format to your code that prints the array:
echo "<pre>";
print_r($final);
echo "</pre>";

Old question, but I generally include the following function with all of my PHP:
The problem occurs because line breaks are not normally shown in HTML output. The trick is to wrap the output inside a pre element:
function printr($data) {
echo sprintf('<pre>%s</pre>',print_r($data,true));
}
print_r(…, true) returns the output without (yet) displaying it. From here it is inserted into the string using the printf function.

Just echo these : echo $_POST['zipcode']."<br/>";

Related

Array to string conversion ecommerce [duplicate]

I have a PHP file that tries to echo a $_POST and I get an error, here is the code:
echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
echo "<input name='C[]' value='$Texting[$i]' " .
"style='background-color:#D0A9F5;'></input>";
}
echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'
Here is the code to echo the POST.
if(!empty($_POST['G'])){
echo $_POST['C'];
}
But when the code runs I get an error like:
Notice: Array to string conversion in
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
What does this error mean and how do I fix it?
When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.
To print properly an array, you either loop through it and echo each element, or you can use print_r.
Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.
What the PHP Notice means and how to reproduce it:
If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:
php> print(array(1,2,3))
PHP Notice: Array to string conversion in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1
Array
In this case, the function print dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.
Another example in a PHP script:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
Correction 1: use foreach loop to access array elements
http://php.net/foreach
$stuff = array(1,2,3);
foreach ($stuff as $value) {
echo $value, "\n";
}
Prints:
1
2
3
Or along with array keys
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
foreach ($stuff as $key => $value) {
echo "$key: $value\n";
}
Prints:
name: Joe
email: joe#example.com
Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']
Correction 2: Joining all the cells in the array together:
In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1,2,3
Correction 3: Stringify an array with complex structure:
In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
print json_encode($stuff);
Prints
{"name":"Joe","email":"joe#example.com"}
A quick peek into array structure: use the builtin php functions
If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose
http://php.net/print_r
http://php.net/var_dump
http://php.net/var_export
examples
$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.
You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Using print, echo on array is not an option anymore.
Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.
Use var_dump,print_r, iterate through input value using foreach or for to output input data for names that are declared as input arrays ('name[]')
Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.
try{ //wrap around possible cause of error or notice
if(!empty($_POST['C'])){
echo $_POST['C'];
}
}catch(Exception $e){
//handle the error message $e->getMessage();
}
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
You can also try like this:
if(isset($_POST['G'])){
if(isset($_POST['C'])){
for($i=0; $i< count($_POST['C']); $i++){
echo $_POST['C'][$i];
}
}
}

There's something wrong with my SQL/PHP prepared statement [duplicate]

I have a PHP file that tries to echo a $_POST and I get an error, here is the code:
echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
echo "<input name='C[]' value='$Texting[$i]' " .
"style='background-color:#D0A9F5;'></input>";
}
echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'
Here is the code to echo the POST.
if(!empty($_POST['G'])){
echo $_POST['C'];
}
But when the code runs I get an error like:
Notice: Array to string conversion in
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
What does this error mean and how do I fix it?
When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.
To print properly an array, you either loop through it and echo each element, or you can use print_r.
Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.
What the PHP Notice means and how to reproduce it:
If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:
php> print(array(1,2,3))
PHP Notice: Array to string conversion in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1
Array
In this case, the function print dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.
Another example in a PHP script:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
Correction 1: use foreach loop to access array elements
http://php.net/foreach
$stuff = array(1,2,3);
foreach ($stuff as $value) {
echo $value, "\n";
}
Prints:
1
2
3
Or along with array keys
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
foreach ($stuff as $key => $value) {
echo "$key: $value\n";
}
Prints:
name: Joe
email: joe#example.com
Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']
Correction 2: Joining all the cells in the array together:
In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1,2,3
Correction 3: Stringify an array with complex structure:
In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
print json_encode($stuff);
Prints
{"name":"Joe","email":"joe#example.com"}
A quick peek into array structure: use the builtin php functions
If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose
http://php.net/print_r
http://php.net/var_dump
http://php.net/var_export
examples
$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.
You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Using print, echo on array is not an option anymore.
Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.
Use var_dump,print_r, iterate through input value using foreach or for to output input data for names that are declared as input arrays ('name[]')
Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.
try{ //wrap around possible cause of error or notice
if(!empty($_POST['C'])){
echo $_POST['C'];
}
}catch(Exception $e){
//handle the error message $e->getMessage();
}
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
You can also try like this:
if(isset($_POST['G'])){
if(isset($_POST['C'])){
for($i=0; $i< count($_POST['C']); $i++){
echo $_POST['C'][$i];
}
}
}

Array to string conversion for calculate discount (not resolved in similar question) [duplicate]

I have a PHP file that tries to echo a $_POST and I get an error, here is the code:
echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
echo "<input name='C[]' value='$Texting[$i]' " .
"style='background-color:#D0A9F5;'></input>";
}
echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'
Here is the code to echo the POST.
if(!empty($_POST['G'])){
echo $_POST['C'];
}
But when the code runs I get an error like:
Notice: Array to string conversion in
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
What does this error mean and how do I fix it?
When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.
To print properly an array, you either loop through it and echo each element, or you can use print_r.
Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.
What the PHP Notice means and how to reproduce it:
If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:
php> print(array(1,2,3))
PHP Notice: Array to string conversion in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1
Array
In this case, the function print dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.
Another example in a PHP script:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
Correction 1: use foreach loop to access array elements
http://php.net/foreach
$stuff = array(1,2,3);
foreach ($stuff as $value) {
echo $value, "\n";
}
Prints:
1
2
3
Or along with array keys
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
foreach ($stuff as $key => $value) {
echo "$key: $value\n";
}
Prints:
name: Joe
email: joe#example.com
Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']
Correction 2: Joining all the cells in the array together:
In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1,2,3
Correction 3: Stringify an array with complex structure:
In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
print json_encode($stuff);
Prints
{"name":"Joe","email":"joe#example.com"}
A quick peek into array structure: use the builtin php functions
If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose
http://php.net/print_r
http://php.net/var_dump
http://php.net/var_export
examples
$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.
You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Using print, echo on array is not an option anymore.
Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.
Use var_dump,print_r, iterate through input value using foreach or for to output input data for names that are declared as input arrays ('name[]')
Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.
try{ //wrap around possible cause of error or notice
if(!empty($_POST['C'])){
echo $_POST['C'];
}
}catch(Exception $e){
//handle the error message $e->getMessage();
}
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
You can also try like this:
if(isset($_POST['G'])){
if(isset($_POST['C'])){
for($i=0; $i< count($_POST['C']); $i++){
echo $_POST['C'][$i];
}
}
}

How to break a file array into string when calling a function [duplicate]

I have a PHP file that tries to echo a $_POST and I get an error, here is the code:
echo "<html>";
echo "<body>";
for($i=0; $i<5;$i++){
echo "<input name='C[]' value='$Texting[$i]' " .
"style='background-color:#D0A9F5;'></input>";
}
echo "</body>";
echo "</html>";
echo '<input type="submit" value="Save The Table" name="G"></input>'
Here is the code to echo the POST.
if(!empty($_POST['G'])){
echo $_POST['C'];
}
But when the code runs I get an error like:
Notice: Array to string conversion in
C:\xampp\htdocs\PHIS\FinalSubmissionOfTheFormPHP.php on line 8
What does this error mean and how do I fix it?
When you have many HTML inputs named C[] what you get in the POST array on the other end is an array of these values in $_POST['C']. So when you echo that, you are trying to print an array, so all it does is print Array and a notice.
To print properly an array, you either loop through it and echo each element, or you can use print_r.
Alternatively, if you don't know if it's an array or a string or whatever, you can use var_dump($var) which will tell you what type it is and what it's content is. Use that for debugging purposes only.
What the PHP Notice means and how to reproduce it:
If you send a PHP array into a function that expects a string like: echo or print, then the PHP interpreter will convert your array to the literal string Array, throw this Notice and keep going. For example:
php> print(array(1,2,3))
PHP Notice: Array to string conversion in
/usr/local/lib/python2.7/dist-packages/phpsh/phpsh.php(591) :
eval()'d code on line 1
Array
In this case, the function print dumps the literal string: Array to stdout and then logs the Notice to stderr and keeps going.
Another example in a PHP script:
<?php
$stuff = array(1,2,3);
print $stuff; //PHP Notice: Array to string conversion in yourfile on line 3
?>
Correction 1: use foreach loop to access array elements
http://php.net/foreach
$stuff = array(1,2,3);
foreach ($stuff as $value) {
echo $value, "\n";
}
Prints:
1
2
3
Or along with array keys
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
foreach ($stuff as $key => $value) {
echo "$key: $value\n";
}
Prints:
name: Joe
email: joe#example.com
Note that array elements could be arrays as well. In this case either use foreach again or access this inner array elements using array syntax, e.g. $row['name']
Correction 2: Joining all the cells in the array together:
In case it's just a plain 1-demensional array, you can simply join all the cells into a string using a delimiter:
<?php
$stuff = array(1,2,3);
print implode(", ", $stuff); //prints 1, 2, 3
print join(',', $stuff); //prints 1,2,3
Correction 3: Stringify an array with complex structure:
In case your array has a complex structure but you need to convert it to a string anyway, then use http://php.net/json_encode
$stuff = array('name' => 'Joe', 'email' => 'joe#example.com');
print json_encode($stuff);
Prints
{"name":"Joe","email":"joe#example.com"}
A quick peek into array structure: use the builtin php functions
If you want just to inspect the array contents for the debugging purpose, use one of the following functions. Keep in mind that var_dump is most informative of them and thus usually being preferred for the purpose
http://php.net/print_r
http://php.net/var_dump
http://php.net/var_export
examples
$stuff = array(1,2,3);
print_r($stuff);
$stuff = array(3,4,5);
var_dump($stuff);
Prints:
Array
(
[0] => 1
[1] => 2
[2] => 3
)
array(3) {
[0]=>
int(3)
[1]=>
int(4)
[2]=>
int(5)
}
You are using <input name='C[]' in your HTML. This creates an array in PHP when the form is sent.
You are using echo $_POST['C']; to echo that array - this will not work, but instead emit that notice and the word "Array".
Depending on what you did with the rest of the code, you should probably use echo $_POST['C'][0];
Array to string conversion in latest versions of php 7.x is error, rather than notice, and prevents further code execution.
Using print, echo on array is not an option anymore.
Suppressing errors and notices is not a good practice, especially when in development environment and still debugging code.
Use var_dump,print_r, iterate through input value using foreach or for to output input data for names that are declared as input arrays ('name[]')
Most common practice to catch errors is using try/catch blocks, that helps us prevent interruption of code execution that might cause possible errors wrapped within try block.
try{ //wrap around possible cause of error or notice
if(!empty($_POST['C'])){
echo $_POST['C'];
}
}catch(Exception $e){
//handle the error message $e->getMessage();
}
<?php
ob_start();
var_dump($_POST['C']);
$result = ob_get_clean();
?>
if you want to capture the result in a variable
You can also try like this:
if(isset($_POST['G'])){
if(isset($_POST['C'])){
for($i=0; $i< count($_POST['C']); $i++){
echo $_POST['C'][$i];
}
}
}

How do I properly use print_r or var_dump?

I use the following snippet quite often when I am debugging:
echo "<pre>" . var_dump($var) . "</pre>";
And I find I usually get a nice readable output. But sometimes I just don't. I'm particularly vexed at the moment by this example:
<?php
$username='xxxxxx';
$password='xxxxxx';
$data_url='http://docs.tms.tribune.com/tech/tmsdatadirect/schedulesdirect/tvDataDelivery.wsdl';
$start=gmdate("Y-m-d\TH:i:s\Z",time());
$stop =gmdate("Y-m-d\TH:i:s\Z",time()+3600*24);
$client = new SoapClient($data_url, array('exceptions' => 0,
'user_agent' => "php/".$_SERVER[SCRIPT_NAME],
'login' => strtolower($username),
'password' => $password));
$data = $client->download($start,$stop);
print_r($data);
?>
I don't want to reveal my credentials of course, but I am told print_r in this case will do the same as my usual snippet when in fact neither print_r nor my snippet produce anything other than runon data with no formatting at all. How can I make it pretty?!
var_dump always shows you an array in formatted data, but too much extra stuff
var_dump($data);
But if you want formatted data, here you need to use <pre> tags:
echo '<pre>';
print_r($data);
echo '</pre>';
var_dump() echos output directly, so if you want to capture it to a variable to provide your own formatting, you must use output buffers:
ob_start();
var_dump($var);
$s = ob_get_clean();
Once this is done the variable $s now contains the output of var_dump(), so we can safely use:
echo "<pre>" . $s . "</pre>";
var_dump is used when you want more detail about any variable.
<?php
$temp = "hello" ;
echo var_dump($temp);
?>
It outputs as follows. string(5) "hello" means it prints the data type of the variable and the length of the string and what is the content in the variable.
While print_r($expression) is used for printing the data like an array or any other object data type which can not directly printed by the echo statement.
Well, print_r() is used to print an array, but in order to display the array in a pretty way you also need HTML tags.
Just do the following:
echo "<pre>";
print_r($data);
echo "</pre>";

Categories