473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

heredoc and array problems

I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on

This works:

$content = <<<TD

{$D['section']}

TD;

Now, I just stumbled on that "fix" and it took me a loooong time to
figure out where the problems laid.

What's going on here?

This isn't much help:

http://us3.php.net/manual/en/languag...syntax.heredoc

Jeff
Jul 17 '08 #1
7 5517
..oO(Jeff)
>I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on
Is display_errors turned on in your php.ini? The above will cause a
parse error. If you use ini_set() to enable display_errors, you won't
see any error message, because the script is not executed.

Micha
Jul 17 '08 #2
You need to look up Interpolation.

Heredoc follows similar interpolation capabilities as echoing with
double quotes.

When you echo an array variable such as $D['section'] in SINGLE QUOTES
you need to concatenate it as single quotes means a string literal
like so:

echo 'I have to escape '.$D['section'].' to output correctly';

When you echo an array variable such as $D['section'] in DOUBLE QUOTES
you do not need to concatenate the array variable (although you can if
you wish) as double quoted strings are INTERPOLATED (or converted)
prior to being written to output.

However, to include an array variable inside an interpolated (double
quoted) string you would leave out the single quotes (which is
perfectly legal syntax) like so:

echo "I don't have to escape $D[section] to output correctly but don't
need the single quotes either!";

The same is true for HEREDOC.

Thus:

$D['section'] = "hello";

echo $content = <<<TD

$D[section]

TD;

would print "hello".

Using curly braces {} is more for outputting variables where the
continuation of the string would make it difficult to interpolate it
properly such as part of a word. Basicly curly braces say: {this is a
variable}

e.g.

$var = 'Talk';

echo "I am $varing";

would not work because $varing is not a defined variable.

echo "I am {$var}ing";

would output "I am Talking";
Hope this helps. :-)
Jul 17 '08 #3
Michael Fesser wrote:
.oO(Jeff)
>I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on

Is display_errors turned on in your php.ini?
I have this at the script top:

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);

Jeff

The above will cause a
parse error. If you use ini_set() to enable display_errors, you won't
see any error message, because the script is not executed.

Micha
Jul 17 '08 #4
macca wrote:
You need to look up Interpolation.

Heredoc follows similar interpolation capabilities as echoing with
double quotes.

When you echo an array variable such as $D['section'] in SINGLE QUOTES
you need to concatenate it as single quotes means a string literal
like so:

echo 'I have to escape '.$D['section'].' to output correctly';

When you echo an array variable such as $D['section'] in DOUBLE QUOTES
you do not need to concatenate the array variable (although you can if
you wish) as double quoted strings are INTERPOLATED (or converted)
prior to being written to output.
OK, I understand that now. Perls heredocs don't do that.
>
However, to include an array variable inside an interpolated (double
quoted) string you would leave out the single quotes (which is
perfectly legal syntax) like so:

echo "I don't have to escape $D[section] to output correctly but don't
need the single quotes either!";
I see that if I do this:

$D[section] = 'some_var';

I get this:
Use of undefined constant section - assumed 'section' in..

So the implied interpolation works only (without notice) inside strings.

Which is a shame as it looks like perl to me without the quotes!
>
The same is true for HEREDOC.

Thus:

$D['section'] = "hello";

echo $content = <<<TD

$D[section]

TD;

would print "hello".
Thanks, I've got it under control now!

Jeff
>
Using curly braces {} is more for outputting variables where the
continuation of the string would make it difficult to interpolate it
properly such as part of a word. Basicly curly braces say: {this is a
variable}

e.g.

$var = 'Talk';

echo "I am $varing";

would not work because $varing is not a defined variable.

echo "I am {$var}ing";

would output "I am Talking";
Hope this helps. :-)
Jul 17 '08 #5
Jeff wrote:
Michael Fesser wrote:
>.oO(Jeff)
>>I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on

Is display_errors turned on in your php.ini?

I have this at the script top:

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);

Jeff

The above will cause a
>parse error. If you use ini_set() to enable display_errors, you won't
see any error message, because the script is not executed.

Micha
As Micha said - it needs to be in your php.ini file (or .htaccess), NOT
IN YOUR SCRIPT.

When you have a syntax error, NOTHING in the script - including the
ini_set(), is executed.
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 17 '08 #6
Jerry Stuckle wrote:
Jeff wrote:
>Michael Fesser wrote:
>>.oO(Jeff)

I have this:

$content = <<<TD

$D['section']

TD;

that fails silently and stops php even though errors are turned on

Is display_errors turned on in your php.ini?

I have this at the script top:

ini_set('display_errors','1');
ini_set('display_startup_errors','1');
error_reporting (E_ALL);

Jeff

The above will cause a
>>parse error. If you use ini_set() to enable display_errors, you won't
see any error message, because the script is not executed.

Micha

As Micha said - it needs to be in your php.ini file (or .htaccess), NOT
IN YOUR SCRIPT.
Hmm, this seems awkward to me as wouldn't it then turn on error
reporting in all scripts? Or should I just set: display_startup_errors
to true in php.ini?

I haven't dug out php.ini yet, I see there seems to be two copies
somewhere off /etc, I believe. One is a backup?

How does the .htaccess bit work?
>
When you have a syntax error, NOTHING in the script - including the
ini_set(), is executed.
Well, I believe I accidentally found a way around this without knowing
what I was doing:

<?php

ini_set('display_startup_errors','1');

include 'script_to_test.php';

?>

I tend to keep my core code in a separate file anyways and the calling
page is just a shell with a few customizations. That's my style
anyways... I didn't do that in the problem case though!

Jeff
>
Jul 18 '08 #7
..oO(Jeff)
>Jerry Stuckle wrote:
>>
As Micha said - it needs to be in your php.ini file (or .htaccess), NOT
IN YOUR SCRIPT.

Hmm, this seems awkward to me as wouldn't it then turn on error
reporting in all scripts?
Correct. On a development machine that's how it's supposed to be. Of
course on a production servers errors should never be shown, but written
to a logfile instead, because they might contain sensitive informations
which you surely don't want to see in the hands of a malicious user.
>Or should I just set: display_startup_errors
to true in php.ini?
Enable all errors and set error_reporting to its highest level.
I haven't dug out php.ini yet, I see there seems to be two copies
somewhere off /etc, I believe. One is a backup?
Check phpinfo() which ini file is in use.

It might also depend on which PHP version you installed and on which
platform. By default the installer comes with multiple different ini
files (three IIRC), but if for example you compile your own PHP from the
sources, you might end up with another ini file in a totally different
location (this happens here on my Debian/Linux box for example).
>How does the .htaccess bit work?
If PHP is installed as a server module, you can use some directives in
an .htaccess file to control various PHP settings. See the manual for
details.

http://www.php.net/manual/en/configuration.changes.php
>When you have a syntax error, NOTHING in the script - including the
ini_set(), is executed.

Well, I believe I accidentally found a way around this without knowing
what I was doing:

<?php

ini_set('display_startup_errors','1');

include 'script_to_test.php';

?>
This works indeed, because the first script is parsed and executed
correctly. The parse error happens in the include file, which will then
kill the interpreter.
>I tend to keep my core code in a separate file anyways and the calling
page is just a shell with a few customizations. That's my style
anyways...
With some more testing scenarios and stricter rules this would be called
a "unit test".

Micha
Jul 18 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: bigoxygen | last post by:
Is it possible to invoke a function from inside heredoc? Thanks
7
by: flupke | last post by:
Hi, i have php script where i use a heredoc type of variabele to print out some html. In the heredoc, i use the values of several other vars. php snippet: $div=<<<END_DIV <DIV...
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
5
by: Tom | last post by:
I've used heredocs for single SQL statements without a problem. Also, I've tried this using the SQL form on PhpMyAdmin and it works so I conclude it should work in PHP. Problem: getting syntax...
6
by: bill | last post by:
Thanks to those that taught me to use heredoc to embed html in php. I don't want to start the html in php vs php in html discussion again., heredoc works a treat for that which I am doing...
2
by: Matt F | last post by:
I can't seem to get heredoc to populate correctly with variables through a form. <textarea name="Template" rows="10" cols="80">Template Here</textarea> Contents could be something like: I want...
2
by: Adam Cantrell | last post by:
How could I count the number of newlines in a heredoc variable? This outputs 0, but I thought heredoc preserved newlines? <? $test = <<<END <a href="test5.php">adam</a> <p> Hi There </p>...
2
by: someusernamehere | last post by:
hi, I have some heredoc on this way: $foo = <<<bar <form action="index.php" method="POST" name="user"> ............................................................................. HTML code...
2
by: ziycon | last post by:
I have a site that has been using the HEREDOC syntax for awhile, after a recent upgrade to the site a lot of pages are erroring with the below error, i can't figure it out at ll, any ideas? All the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.