Log in

View Full Version : Proper php usage for .= <<<END... END;



kuau
02-09-2011, 03:36 AM
Does anyone know the proper usage of this php structure? I have Googled it and cannot find anything... not sure what it is called.


$variable .= <<<END
contents
END;


It seems as if it may be a more elegant way to assign multi-line values to a variable but I get an error message when I try it. Not sure of the correct format for the contents. Can you include variables? Do you enclose text in quotes? No clue. Thanks.

djr33
02-09-2011, 03:51 AM
http://php.net/manual/en/language.types.string.php
Look for the section on "Heredoc syntax".

That's a weird structure that allows you to avoid escaping. If you are using something that contains many ' and " then it might make sense. But realistically I don't see the point in using it because it is so odd.

I don't see any typos in your code, but maybe it isn't capable of performing the .= operation. Try just = instead to see if that works. It also might not be installed/enabled in your PHP configuration. I don't know anything about this specific issue, but hopefully this will help you start narrowing it down.

The effect is that <<<XYZ .......... XYZ; is basically the same as "............" but just fancier.

Note that you can use new lines in a string easily. Just use a new line inside the quotes just like you'd add any other character. If you would prefer to keep your code on one line, you can use \n as a line break (or \r\n or just \r\ depending on the OS) within double quotes (it's literally \n in single quotes).



EDIT: Take a look at "Nowdoc" on the same page. That might be more useful for your purposes because it looks like the Heredoc syntax is limited in some ways.

traq
02-09-2011, 07:52 AM
The only practical use I've ever had for heredocs is when I'm using php to dynamically generate javascript code (where I'm likely to use both single and double quotes, and don't want to keep escaping them). But I try to avoid it- weirder than it's worth. :D

kuau
02-09-2011, 12:25 PM
This is it! Thanks so much. So if I read it correctly, I don't need to use quotes or dots to concatenate text and variables? So would this be correct?


$body = <<<END
Name: $_POST['name']\n;
Email: $_POST['email']\n;
Phone: $_POST['phone']\n;
END;

It's to replace this:

$body1 = "Name: ".$_POST['name']."\n";
$body1.= "Email: ".$_POST['email']."\n";
$body1.= "Phone: ".$_POST['phone']."\n";
$body1.= "Agent: ".$_POST['agent']."\n";

That's just a few lines of the code. I'm simply trying to find the best (simplest, cleanest, most efficient) way to create the body of an email.

Mahalo for your help! :)

bluewalrus
02-09-2011, 02:05 PM
You could also do


$body1 = "Name: {$_POST['name']} \n";
$body1.= "Email: {$_POST['email']} \n";
$body1.= "Phone: {$_POST['phone']} \n";
$body1.= "Agent: {$_POST['agent']} \n";

traq
02-09-2011, 03:35 PM
$body = <<<END
Name: $_POST['name']\n;
Email: $_POST['email']\n;
Phone: $_POST['phone']\n;
END;
unfortunately, that gets you this:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING [...]

Complex variables (read: indexes) still mess things up. Bluewalrus' {brackets suggestion} actually fixes it:
$body = <<<END
Name: {$_POST['name']}\n;
Email: {$_POST['email']}\n;
Phone: {$_POST['phone']}\n;
END;


As a side note - and your ending sequence was correct, kauu, but I can see this being overlooked - you will also get a parse error if your END; is not followed immediately by a line break. So, this is correct:
END;
// there's another line right here
but this is not:
END; //this is the last line in the script (also, there's whitespace and a comment after the END; )

kuau
02-09-2011, 10:19 PM
I'm not familiar with using curly brackets except in css. What do they mean here? Thanks.

djr33
02-09-2011, 11:18 PM
They're similar to the brackets in an if statement.

An if statement takes a single statement. For example:
if ($x==1) echo '1';

In order to make it take more than that one statement, you use brackets to surround them and make them act like one:
... { echo '1'; echo '2'; }
The brackets contain 2 statements, but they are read together (with the brackets) as 1 statement.

Now after that tangent, this particular instance is a bit of an exception but it follows basically the same idea. It tells PHP to group the characters between the brackets as a single unit.

In double quotes (or heredoc structures) you can include variables:
echo "$foo";

But you can't use an array index in exactly that way because it tries to parse it as literal text and gets confused. So you help PHP by telling it that you are using the array and index as a unit:
echo "{$foo['bar']}";

Of course in that example it seems like overkill, but in something like what you've posted or a longer string it makes sense to have a way to include variables within the string.